在MDI父表单上的子表单关闭或出现时显示/隐藏,BringToFront / SendToBack在父表单上显示一个面板

时间:2011-05-18 06:09:34

标签: c# .net winforms panel

当MDI父表格上的儿童表格关闭时,我需要在父表格上隐藏一个面板。当子表单关闭时,在父表单上显示该面板。

目前我正在使用SendtoBack()来显示父窗体上面板的子窗体,但当我关闭子窗体时,即使我使用了面板,面板也不会显示:

BringtoFront()

OR

Panel1.Visible=true


    public partial class CHILD : Form
        {
      private void CHILD_Load(object sender, EventArgs e)
            {
                this.FormClosed += new FormClosedEventHandler(CHILD_FormClosed);
            }

     void CHILD_FormClosed(object sender, FormClosedEventArgs e)
            {
                PARENTForm P = new PARENTForm();
                P.panel1.BringToFront();
                P.panel1.Visible = true; 

            }
}




public partial class Form1 : Form
   {
   private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
            {
                CHILD P = new CHILD();
                P.Showg();
                P.MdiParent = this;
                P.BringToFront();
                panel1.SendToBack();
                panel1.Visible = false;
            }
    }

这不工作....请帮助..!

enter image description here

2 个答案:

答案 0 :(得分:3)

您以子窗体形式创建新的父窗体。您需要将父表单对象传递给子表单,然后使用它来显示/隐藏面板并将面板Modifiers属性设置为public。 例如......

父母表格:

public partial class ParentForm : Form
{
    public ParentForm()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        panel1.Visible = false;
        ChildForm childForm = new ChildForm();
        childForm.MdiParent = this;
        childForm.Show();
    }
}

儿童表格:

public partial class ChildForm : Form
{
    public ChildForm()
    {
        InitializeComponent();
    }

    private void Child_FormClosed(object sender, FormClosedEventArgs e)
    {
        ParentForm parentForm = (ParentForm)this.MdiParent;
        parentForm.panel1.Visible = true;
    }
}

答案 1 :(得分:0)

使用此代码 家长形式

    private void MainMenu_ChildForm_Click(object sender, EventArgs e)
    {
            ChildForm frm = new ChildForm();
            frm.MdiParent = this;
            ShowHideMainPanel(frm);
            frm.Show();
    }

void ShowHideMainPanel(Form frm)
        {
            frm.FormClosed += new FormClosedEventHandler(Form_Closed);
            panel1.Visible = false;
        }

        void Form_Closed(object sender, FormClosedEventArgs e)
        {
            panel1.Visible = true;
        }

通过为子项分配关闭事件以使其在关闭后显示面板