如何在子表单处于活动状态时禁用MDIparent表单 [英] How to disable the MDIparent form when a child form is active

查看:68
本文介绍了如何在子表单处于活动状态时禁用MDIparent表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi there
Am a new student in C# and am using Microsoft Visual C# 2013.
My aim is to 
i. open a child form inside its MDIparent form
ii. disable the MDIparent form when the child form is active/opened

it was easier in VB.net with
<pre lang="c#">frmStuDetails.ShowDialog()





我试过了



1.



I have tried

1.

MyChildForm childForm = new MyChildForm();
childForm.ShowDialog(this);





结果。 ...但问题是子表单不能在MDIparent表单/容器中打开



2.

在MDIparent调用下按钮



Result....but the problem is that the child form doesn't open within the MDIparent form/container

2.
under MDIparent call button

frmViewStuList childForm = new frmViewStuList(this);
childForm.Owner = this;
childForm.Show();





来自Activated的孩子



under child from Activated

 if (this.Owner != null)
  {
    this.Owner.Enabled = false;
  }

under child form Deactivate
 if (this.Owner != null)
 {
   this.Owner.Enabled = true;
 }





结果.....它会使子表单处于活动状态,但在子表单关闭时会冻结MDIparent br $>


3.



Result.....it makes the child form active but freezes the MDIparent when the child form closes

3.

ChildForm child = new ChildForm();
child.Owner = this;
child.Show();

// In ChildForm_Load:
private void ChildForm_Load(object sender, EventArgs e) 
{
  this.Owner.Enabled = false;
}

private void ChildForm_Closed(object sender, EventArgs e) 
{
  this.Owner.Enabled = true;
} 





结果......这似乎是最好的选择,但是儿童表格不能在MDIparent



如果您有任何其他想法,请提供帮助

谢谢



Result ....It seems to be the best option but the child form doesn't open within the MDIparent

Please help if you have any other idea
Thanks

推荐答案

首先,如果您需要禁用整个 MDIParent 表单,您可能不需要它 MDIParent 。但是,一个有效的方案可能是您只需要一个子窗体实例一直打开。现在可以使用这两种方式中的任何一种来实现(除了这两种方式之外还有更多方法):



1.禁用显示子表单的控件子表单已打开,并在子表单关闭后重新激活它。在下面的代码中,该按钮可打开子窗体。在父表单中,您可以这样:



First of all, if you need to disable the entire MDIParent form, you probably don't need it to be MDIParent. However, one valid scenario could be that you need only one instance of child form to be open all the time. Now this can be achieved using either of these tow ways (there may be more ways apart from these two):

1. Disable the control that displays the child form when child form is open and reactivate it once child form is closed. In code below, the button opens the child form. In the parent form you can have this:

public bool EnableButton
       {
           set
           {
               button1.Enabled = value;
           }
       }

       private void button1_Click(object sender, EventArgs e)
       {

               Child child = new Child();
               child.MdiParent = this;
               button1.Enabled = false;
               child.Show();

       }



并以子形式:




And in the child form:

private void Child_FormClosing(object sender, FormClosingEventArgs e)
       {
           (this.MdiParent as Parent).EnableButton = true;
       }





2.当用户点击按钮打开子表单时,请检查它是否已存在。如果是,请再次显示该表单。这可以再次以各种方式完成。我在子表单中使用私有变量,并在样本中使用 Application.OpenForms 集合。





2. When the user clicks on the button to open the child form, check if it already exists. If yes, just show that form again. This could again be done in various ways. I am using private variable for child form and also Application.OpenForms collection in sample.

Child _child;
       private void button1_Click(object sender, EventArgs e)
       {
           #region Using OpenForms Collection
           bool isAlreadyOpen = false;

           foreach (Form form in Application.OpenForms)
           {
               if (form.GetType() == typeof(Child))
               {
                   isAlreadyOpen = true;
                   if (form.WindowState == FormWindowState.Minimized)
                       form.WindowState = FormWindowState.Normal;

                   form.Focus();
                   break;
               }
           }

           if (!isAlreadyOpen)
           {
               Child child = new Child();
               child.MdiParent = this;
               child.Show();
           }
           #endregion

           #region PrivateMember
           if (_child != null)
           {
               if (_child.WindowState == FormWindowState.Minimized)
                   _child.WindowState = FormWindowState.Normal;

               _child.Focus();
           }
           else {
               _child = new Child();
               _child.MdiParent = this;
               _child.Show();
           }
           #endregion

       }





检查类型可能非常耗时,如果申请中有许多开放表格,搜索可能需要一段时间。



Checking for types can be time consuming and also if there are many open forms in application, it may take a while to search.


这篇关于如何在子表单处于活动状态时禁用MDIparent表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆