1

Is there a WinForms control that can show and hide another WinForms control similar to the concept of a TreeNode collapse button?

Simon
  • 7,991
  • 21
  • 83
  • 163
  • 1
    Yes, there is... and you can make your own too. But it all depends on your specific needs that one or another would work for you – Jcl Jul 12 '16 at 10:31
  • You can use `Show` and `Hide` methods of the control to show or hide it. The methods can be called using any part of your code, for example a button click. What's your requirement? – Reza Aghaei Jul 12 '16 at 10:31
  • What about an expander? http://stackoverflow.com/questions/3795005/add-an-expander-collapse-expand-to-a-panel-winform – Andez Jul 12 '16 at 10:45

3 Answers3

1

The easy way to do it is add a button and a checkbox to a form, add an event handler for the checkbox CheckedChanged event and in the event handler code simply add:

button1.Visible = !checkBox1.Checked;

The better way to do it would be with data binding and INotifyProperyChanged

Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60
1

Do you mean something similar to an Accordian?

See this post winforms accordion

Community
  • 1
  • 1
Stuart Meeks
  • 173
  • 8
0

You could write wrappers for all the controls you want to use and make all their visibilities depend on the state of another control.

    private delegate void ToggleVoid();
    private static event ToggleVoid VisibilityToggle;

    private void Form1_Load(object sender, EventArgs e)
    {
        DependantButton TestButton = new DependantButton();
        TestButton.SetBounds(100, 100, 100, 100);
        this.Controls.Add(TestButton);

        Button ToggleButton = new Button();
        ToggleButton.SetBounds(200, 200, 100, 100);
        ToggleButton.Click += OnToggleButtonClicked;
        this.Controls.Add(ToggleButton);
    }

    private void OnToggleButtonClicked(object sender, EventArgs e)
    {
        VisibilityToggle.Invoke();
    }

    private class DependantButton : Button
    {
        public DependantButton() : base()
        {
            VisibilityToggle += ToggleVisibility;
        }

        public void ToggleVisibility()
        {
            Visible = !Visible;
        }
    }
Jackson Tarisa
  • 298
  • 2
  • 9
  • I'm spotting a theme here: you're going around adding non-answers to quite a few questions. See https://stackoverflow.com/help/how-to-answer – Nick May 27 '18 at 16:09