简体   繁体   中英

Is there a WinForms control to show and hide another WinForms control?

是否有一个WinForms控件可以显示和隐藏另一个类似于TreeNode折叠按钮概念的WinForms控件?

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

Do you mean something similar to an Accordian?

See this post winforms accordion

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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM