显示子窗体时隐藏父窗体的面板控件

本文关键字:窗体 控件 隐藏 显示 | 更新日期: 2023-09-27 18:28:02

当单击GroupSelect childform中打开另一个child form GroupExmStart的按钮时,当这个GroupExmStart窗体打开时,panel4应该隐藏,当它关闭时,它应该可见,我试图隐藏parent Form2的Panel控件,但它不起作用,也没有发生任何事情。我哪里出了问题,我怎么才能用正确的方法呢?

父窗体

public partial class Form2 : Form
{
    public Control control
    {
        //using this I accessed panel4 in child form GroupSelect
        get {return this.panel4; }
    }
}

子窗体

public partial class GroupSelect : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2(lgnName);
        frm2.panel4.Visible = false;
        GroupExmStart grpexamfrm = new GroupExmStart(GrpID, DurationID, lgnName);
        grpexamfrm.MdiParent = this.ParentForm;
        //showing another child form and 
        grpexamfrm.Show();
    }
}

显示子窗体时隐藏父窗体的面板控件

您正在创建新的form2,但据我所知,您的父窗体是form2,因此您可以按照以下进行操作

private void button1_Click(object sender, EventArgs e)
{ 
   var frm2  = this.Parent as Form2;
   if(frm2 !=null)
        frm2.control.Visible = false;
   GroupExmStart grpexamfrm = new GroupExmStart(GrpID, DurationID,lgnName);
   grpexamfrm.MdiParent = this.ParentForm;
   grpexamfrm.Show();//showing another child form and 
}

此代码对我的非常有效

在父窗体中

public Form2(string userName)
        {
        InitializeComponent();            
        panelHide = panel4;
        }
public static Panel panelHide = new Panel();

在组中选择子窗体

private void button1_Click(object sender, EventArgs e)
        {
            Form2.panelHide.Hide();
        }