且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中隐藏/显示Windows窗体面板

更新时间:2023-12-06 09:44:52

不要使用标志,因为您的按钮行为将由标志的状态决定.

Don't use flags, because your button behavior will be determined by the states of the flags.

***是按照您想要的方式对其进行编码.如果希望每个Button都使相应的面板可见,而其他面板不可见:

Best is to code it the way you want. If you want each Button to make the corresponding panel visible while other panel invisible:

private void button1_Click(object sender, EventArgs e)
{
     panel1.Visible = true;
     panel2.Visible = false;
     //Application.DoEvents();
}

private void button2_Click(object sender, EventArgs e)
{
     panel2.Visible = true;
     panel1.Visible = false;
     //Application.DoEvents();
}

或者,如果您希望每个按钮独立控制每个面板的可见性,请执行以下操作:

Or, if you want each button to control the visibility of each panel independently, do this:

private void button1_Click(object sender, EventArgs e)
{
     panel1.Visible = !panel1.Visible;
     //Application.DoEvents();
}

private void button2_Click(object sender, EventArgs e)
{
     panel2.Visible = !panel2.Visible;
     //Application.DoEvents();
}

最后,可以删除Application.DoEvents()(向 Thorsten Dittmar 贷记),因为控件将立即返回无论如何,在Click方法完成后将其添加到UI线程.阅读他的评论和引用的

Lastly, the Application.DoEvents() can be removed (credit to Thorsten Dittmar) as the control will immediately back to UI thread after the Click method finishes anyway. Read his comment and the referred link.