Hide/Show Windows C# 中的表单面板
Hide/Show Windows Forms panel in C#
我尝试在 C# 中 show/hide 面板,但是当我单击 button1 时我想看到 panel1 但出现了 panel2。
当我点击 button2 时,panel2 消失了。
但是当我先点击button2时,panel2没有出现。
我不知道我的代码有什么问题,但它是:
public Form3()
{
InitializeComponent();
}
bool show1;
bool show2;
private void button1_Click(object sender, EventArgs e)
{
if(show1)
{
panel1.Visible = false;
show1 = false;
}
else
{
panel1.Visible = true;
show1 = true;
}
Application.DoEvents();
}
private void button2_Click(object sender, EventArgs e)
{
if (!show2)
{
panel2.Visible = true;
show2 = true;
}
else
{
panel2.Visible = false;
show2 = false;
}
Application.DoEvents();
}
不要使用标志,因为您的按钮行为将由标志的状态决定。
最好按照您想要的方式进行编码。如果您希望每个 Button
使相应的面板可见而其他面板不可见:
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();
}
或者,如果您希望每个按钮独立控制每个面板的可见性,请执行以下操作:
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) as the control will immediately back to UI thread after the Click
method finishes anyway. Read his and the referred link。
不要使用 show1
和 show2
这样的全局变量
你可以这样做。
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = !panel1.Visible;
Application.DoEvents();
}
无需使用变量和条件。
只需在按钮点击时添加 panel1.Visible = false; or panel1.Visible = true;
您可能会找到解决方案,但我只想建议您在更改控件状态时使用 SuspendLayout()
和 ResumeLayout()
来提高性能
private void button1_Click(object sender, EventArgs e)
{
this.SuspendLayout();
panel1.Visible = true;
panel2.Visible = false;
this.ResumeLayout();
//Application.DoEvents();
}
private void button2_Click(object sender, EventArgs e)
{
this.SuspendLayout();
panel2.Visible = true;
panel1.Visible = false;
this.ResumeLayout();
//Application.DoEvents();
}
我尝试在 C# 中 show/hide 面板,但是当我单击 button1 时我想看到 panel1 但出现了 panel2。 当我点击 button2 时,panel2 消失了。 但是当我先点击button2时,panel2没有出现。 我不知道我的代码有什么问题,但它是:
public Form3()
{
InitializeComponent();
}
bool show1;
bool show2;
private void button1_Click(object sender, EventArgs e)
{
if(show1)
{
panel1.Visible = false;
show1 = false;
}
else
{
panel1.Visible = true;
show1 = true;
}
Application.DoEvents();
}
private void button2_Click(object sender, EventArgs e)
{
if (!show2)
{
panel2.Visible = true;
show2 = true;
}
else
{
panel2.Visible = false;
show2 = false;
}
Application.DoEvents();
}
不要使用标志,因为您的按钮行为将由标志的状态决定。
最好按照您想要的方式进行编码。如果您希望每个 Button
使相应的面板可见而其他面板不可见:
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();
}
或者,如果您希望每个按钮独立控制每个面板的可见性,请执行以下操作:
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) as the control will immediately back to UI thread after the Click
method finishes anyway. Read his
不要使用 show1
和 show2
这样的全局变量
你可以这样做。
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = !panel1.Visible;
Application.DoEvents();
}
无需使用变量和条件。
只需在按钮点击时添加 panel1.Visible = false; or panel1.Visible = true;
您可能会找到解决方案,但我只想建议您在更改控件状态时使用 SuspendLayout()
和 ResumeLayout()
来提高性能
private void button1_Click(object sender, EventArgs e)
{
this.SuspendLayout();
panel1.Visible = true;
panel2.Visible = false;
this.ResumeLayout();
//Application.DoEvents();
}
private void button2_Click(object sender, EventArgs e)
{
this.SuspendLayout();
panel2.Visible = true;
panel1.Visible = false;
this.ResumeLayout();
//Application.DoEvents();
}