如果子窗体已经打开,我该如何关闭呢? [英] How can I go about closing a Child form if it's already open?

查看:76
本文介绍了如果子窗体已经打开,我该如何关闭呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果子窗体已经打开,我该如何关闭呢?如果尚未打开,则将其打开吗?

How can I go about closing a Child form if it''s already open? And if it isn''t already open, then open it?

推荐答案

首先,不存在诸如父子形式的关系.


有拥有的表单"和所有者表单".您确实需要使用这种关系;它会影响激活行为(同一应用程序的所有形式都将一起打开激活).您需要使用System.Windows.Forms.Form.Owner.出于明显的原因,最好使所有表单归主表单所有,并为所有拥有的表单将Form.ShowInTaskbar设置为false.

现在,您需要打开,关闭和重新打开非主表单(我们希望同意拥有表单),您需要实现以下行为:隐藏表单而不是关闭:

First of all, there is not such relationship as child-parent forms.


There are Owned Forms and Owner Forms. You really need to use this relationship; it affect activation behavior (all forms of the same application will open activate together). You need to use System.Windows.Forms.Form.Owner. It''s good to make all forms owned by the main form and set Form.ShowInTaskbar to false for all owned form, by the apparent reason.

Now, of you need to open, close and reopen non-main (as we hopefully agree, owned forms), you need to implement the following behavior: hide form instead of closing:

protected override void OnFormClosing(FormClosingEventArgs e) {
   if (e.CloseReason == CloseReason.UserClosing) {
       e.Cancel = true;
       Hide();
   } //if
} //OnFormClosing



请注意检查CloseReason:该行为仅适用于尝试由用户关闭表单.

显示可以是懒惰的,但是无论表单是否显示状态都没有关系,是否可以当前显示:



Pay attention for check up of CloseReason: the behavior will be applied only to attempt to close the form by the user.

The show can be lazy, but regardless to the show state of the form does not matter, if can be currently shown or not:

MyForm MyForm;

//...

if (MyForm == null) {
   MyForm = new MyForm();
   this.AddOwnedForm(MyForm); //if "this" is a main form
   MyForm.ShowInTaskbar = false;
} //if
MyForm.Show(); //unconditional: if already shown, it's fine.



—SA



—SA


在父表单中,您必须创建一个实例才能显示子表单.您可以使用该实例查看子表单的状态.
Form a parent form you have to create an instance to be able to show the child form. You can use that instance to see the state of the child form.


这篇关于如果子窗体已经打开,我该如何关闭呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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