在Windows Form中.现在主窗口内有三个Panel 例如我写了一个方法public void HideALLPanelAndShowThisPanel(Panel panel)
{
   ///这里将所有panel设为隐藏
   ///例如:panel1.Visible=false;
   ///然后将传递来的panel设为显示
   ///panel.Visible=true;
}但是当我debug的时候,即便是走完了panel.Visible=true;这条代码,这个panel的属性却依然是false的.
我哪里出错了?望各位大侠帮忙.

解决方案 »

  1.   


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

      

  2.   

    谢谢楼上的  不过你的效果跟我写的效果是一样的 依然是false
      

  3.   

            private void PanelControl(Panel panel) 
            {
                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
      

  4.   

    我明白了 是panel之间的级别问题.
      

  5.   

    LZ应该是panel的层次关系问题。。要弄清楚谁是最上层,谁是最底层
      

  6.   


    用递补归方法,适用多层容器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);  // 加上本句 解决多层递归
            }
        }

      

  7.   

    6楼方法更正:用递归方法,适用多层容器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.Name != panel.Name && c.Visible && (c is Panel)) c.Visible = false;
            if (c.Controls.Count > 0) HideALLPanelAndShowThisPanel(c, panel);  // 加上本句 解决多层递归
        }