Question

I am trying to create a custom collapsible panel for use in one of my projects. I have it set up to display which sides should be able to collapse, and it takes a string input {"None", "Up", "Right", "Down", "Left", "All"};.

Here is what I have so far:

public partial class CollapsiblePanel : UserControl
{
    # region Collapse Direction
    List<string> DirectionOptions = new List<string> { "None", "Up", "Right", "Down", "Left", "All" };
    [Browsable(true), DefaultValue("All"), Description("Direction panel collapses. 0-none, 1-up, 2-right, 3-down, 4-left, 5-all")]
    [ListBindable(true), Editor(typeof(ComboBox), typeof(UITypeEditor))]
    private string _direction = "All";
    public List<string> Direction
    {
        get { return DirectionOptions; }
        set
        {
            DirectionOptions = value;
            callCollapseDirectionChanged();
        }
    }

    public event Action CollapseDirectionChanged;
    protected void callCollapseDirectionChanged()
    {
        Action handler = CollapseDirectionChanged;
        DisplayButtons();
        if (handler != null)
        {
            handler();
        }
    }
    # endregion

    public CollapsiblePanel()
    {
        InitializeComponent();
    }

    private void DisplayButtons()
    {
        switch (_direction)
        {
            case "None":
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case "Up":
                buttonDown.Visible = false;
                buttonUp.Visible = true;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case "Right":
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = true;
                buttonLeft.Visible = false;
                break;

            case "Down":
                buttonDown.Visible = true;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case "Left":
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = true;
                break;

            case "All":
                buttonDown.Visible = true;
                buttonUp.Visible = true;
                buttonRight.Visible = true;
                buttonLeft.Visible = true;
                break;
        }
    }
}

Can someone explain to me how to get the designer to give the user the DirectionOptions list as the possible values? They can pick any one of the strings as the value.

Was it helpful?

Solution

Should the user be able to select multiple options or just one?

If its just one - then the property should be an Enum not a List.

Something like this should work:

public partial class CollapsiblePanel : UserControl
{
    public enum CollapseDirection
    {
        None,
        Up,
        Right,
        Down,
        Left,
        All
    }

    # region Collapse Direction

    [Browsable(true), DefaultValue("All"), Description("Direction panel collapses. 0-none, 1-up, 2-right, 3-down, 4-left, 5-all")]
    [ListBindable(true), Editor(typeof(ComboBox), typeof(UITypeEditor))]
    private CollapseDirection _direction = CollapseDirection.All;
    public CollapseDirection Direction
    {
        get { return _direction; }
        set
        {
            _direction = value;
            callCollapseDirectionChanged();
        }
    }

    public event Action CollapseDirectionChanged;
    protected void callCollapseDirectionChanged()
    {
        Action handler = CollapseDirectionChanged;
        DisplayButtons();
        if (handler != null)
        {
            handler();
        }
    }
    # endregion

    public CollapsiblePanel()
    {
        InitializeComponent();
    }

    private void DisplayButtons()
    {
        switch (_direction)
        {
            case CollapseDirection.None:
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case CollapseDirection.Up:
                buttonDown.Visible = false;
                buttonUp.Visible = true;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case CollapseDirection.Right:
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = true;
                buttonLeft.Visible = false;
                break;

            case CollapseDirection.Down:
                buttonDown.Visible = true;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case CollapseDirection.Left:
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = true;
                break;

            case CollapseDirection.All:
                buttonDown.Visible = true;
                buttonUp.Visible = true;
                buttonRight.Visible = true;
                buttonLeft.Visible = true;
                break;
        }
    }
}
  • Note - this is guide - I haven't tested the code, but that should put you in the general direction of what you want to achieve.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top