在Windows Form中.现在主窗口内有三个Panel 例如我写了一个方法public void HideALLPanelAndShowThisPanel(Panel panel)
{
///这里将所有panel设为隐藏
///例如:panel1.Visible=false;
///然后将传递来的panel设为显示
///panel.Visible=true;
}但是当我debug的时候,即便是走完了panel.Visible=true;这条代码,这个panel的属性却依然是false的.
我哪里出错了?望各位大侠帮忙.
{
///这里将所有panel设为隐藏
///例如:panel1.Visible=false;
///然后将传递来的panel设为显示
///panel.Visible=true;
}但是当我debug的时候,即便是走完了panel.Visible=true;这条代码,这个panel的属性却依然是false的.
我哪里出错了?望各位大侠帮忙.
private void button1_Click(object sender, EventArgs e)
{
HideALLPanelAndShowThisPanel(panel1);
}public void HideALLPanelAndShowThisPanel(Panel panel)
{
foreach (Control c in this.Controls)
{
if (c is Panel)
{
c.Visible = (c.Name == panel.Name) ? true : false;
}
}
}
{
panelIndex.Visible = false;
MessageBox.Show("panelIndex" + Convert.ToString(panelIndex.Visible));
panelAddCust.Visible = false;
MessageBox.Show("panelAddCust"+Convert.ToString(panelAddCust.Visible));
panelCustList.Visible = false;
MessageBox.Show("panelCustList" + Convert.ToString(panelCustList.Visible));
panel.Visible = true;
MessageBox.Show(panel.Name+Convert.ToString(panel.Visible));
}所有的提示结果都是false
用递补归方法,适用多层容器private void button4_Click(object sender, EventArgs e)
{
HideALLPanelAndShowThisPanel(this, panel4);
}public void HideALLPanelAndShowThisPanel(Control ctrl, Panel panel)
{
foreach (Control c in ctrl.Controls)
{
if (c is Panel)
{
if (c.Name != panel.Name && c.Visible) c.Visible = false;
if (c.Controls.Count > 0) HideALLPanelAndShowThisPanel(c, panel); // 加上本句 解决多层递归
}
}
}
{
HideALLPanelAndShowThisPanel(this, panel4);
}public void HideALLPanelAndShowThisPanel(Control ctrl, Panel panel)
{
foreach (Control c in ctrl.Controls)
{
if (c.Name != panel.Name && c.Visible && (c is Panel)) c.Visible = false;
if (c.Controls.Count > 0) HideALLPanelAndShowThisPanel(c, panel); // 加上本句 解决多层递归
}
}