简体   繁体   English

Windows窗体中的C#组框可见性

[英]C# groupbox visibility in windows form

I Googled around but seems my problem happens when two gropboxes are overlapping, in my case they are not overlapping! 我在Google周围搜索,但似乎我的问题在两个gropbox重叠时发生,在我的情况下它们不重叠! Problem is that the Visible property of groupbox doesn't work. 问题是groupbox的Visible属性不起作用。 what am I trying to do is that groupbox1 is visible when program starts and groupbox2 is not, by clicking on a button it should goes invisible and groupbox2 should appear, clicking the same button this action should be done vice versa. 我想做的是,当程序启动时groupbox1是可见的,而groupbox2不可见,通过单击一个按钮,它应该变为不可见,而应显示groupbox2,单击同一按钮,则应执行此操作,反之亦然。

here is my code: 这是我的代码:

    private void button2_Click(object sender, EventArgs e)
    {
        if (groupBox2.Visible == false)
        {
            groupBox1.Visible = false;
            groupBox2.Visible = true;
        }
        if (groupBox1.Visible == false)
        {
            groupBox1.Visible = true;
            groupBox2.Visible = false;
        }

    }

Your problem is that after the first if -statement, it immediately checks if groupBox1.Visible is false , which it always will be. 您的问题是,在第一个if -statement之后,它会立即检查groupBox1.Visible是否为false ,并且总是如此。 It then proceeds to flip it back. 然后将其翻转回去。

Change the if to an else , or at least and else if and your code will work. 更改ifelse ,或者至少和else if你的代码将工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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