如果表单已经打开,如何重新打开它
你好朋友
我想知道如何检测表格是否已打开.如果窗体已打开,则将其关闭并重新打开它.
在我的应用程序中,我使用单一"表单查看客户信息并创建新的客户信息.
如果用户已经打开了客户表单,然后再次尝试打开IT,那么我想这样做,那么以前打开的表单应该关闭,并且新的表单实例将被打开.
请帮我.
硼酸丁酯
Equilux Technologies
Hello Frnds
I want to know how to detect is form already opened. If form is opened then close it and Reopen IT.
In my application I am using Single form to view customer information and create new customer Information.
I want to do if user has already opened a customer form and again tries to open IT then Previously opened form should be close and new instance of form will be opened .
Plz Help Me.
Nitin bhoyate
Equilux Technologies
请看下面的代码.
基本上我有一个带有2个按钮的表单,一个运行测试,另一个打开另一个表单的实例.
使用OpenForms集合,我们可以检查表单是否已打开;
Look at this code.
Basically i have a form with 2 buttons on it, one runs a test, the other opens an instance of an other form.
Using the OpenForms collection we can check if the form already is open;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonOpenOtherForm_Click(object sender, EventArgs e)
{
Form theOtherOne = new FormTheOtherForm();
theOtherOne.Name = "SomeFormName";
theOtherOne.Show();
}
private void buttonTest_Click(object sender, EventArgs e)
{
foreach (Form aForm in Application.OpenForms)
{
if (aForm.Name.Equals("SomeFormName"))
{
MessageBox.Show("SomeFormName Already Open, closing and repopening next");
aForm.Close();
buttonOpenOtherForm.PerformClick();
break;
}
}
}
}
首先,即使可以关闭窗体,也不存在打开"操作(因此无法知道是否可以打开)表单已经打开" :-). Form的实例通过其构造函数实例化,然后显示,然后关闭.
一切都取决于您如何关闭表单.我认为最好的关闭方法是隐藏表格.为此,请覆盖:
First of all, even though a form can be closed, there is not such thing as "Open" operation (and, hence, it is not possible to know if a form is "already opened" :-). An instance of a Form is instantiated via its constructor, then shown, then closed.
Everything depends on how you close the form. I think the best way of closing is hiding the form; to do this, override :
protected override void OnFormClosing(FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing) {
e.Cancel = true;
Hide();
} else
base.OnFormClosing(e);
}
您可以通过处理事件FormClosing
来获得相同的效果,但是实现覆盖更容易.
有关关闭表单的详细信息,请参见 http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close.aspx [
You can get the same effect by handling the event FormClosing
, but override is implemented easier.
For more detail on what happens on closing the form, please see http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close.aspx[^]. As you can see, hiding the form is the recommended way of handling this situation. Note, that a closed form is disposed (see below).
In this way, only one operation is needed: Form.Show
.
If you need the form to be really closed, you can test the predicate Form.IsDisposed
. To show the form again, you will need to instantiate an instance of the form again and then show new instance, as a disposed cannot be shown. Another way is checking Application.OpenForms
and trying to find a form in question in the collection.
I do not recommend closing of the form by the user; hiding is much better. When a form is hidden, it preserved its status and status of all the controls, when a form is shown again. Don''t think you save memory on closing the form. You don''t.
Don''t forget the ownership relationship between forms. It is better to make non-main forms owned by main forms:
Form MyOwnedForm = new CustomerInformationForm();
MyOwnedForm.Owner = MyMainForm;
MyOwnedForm.ShowInTaskbar = false; //also very useful: only one task bar icon per application
MyOwnedForm.Show(MyMainForm);
这将保证同一应用程序形式的一致性激活,应与ShowInTaskbar = false
一起使用以保持一致性.
最终建议:最好将每个应用程序的表单数量保持在最低限度,而更复杂的表单则是最佳选择.使用选项卡式UI,停靠UI或类似的方法.
This will guarantee coherent activation of the forms of the same application, should be used with ShowInTaskbar = false
for consistency.
A final recommendation: it''s the best to keep number of forms per application to bare minimum, with one more complex form being the best option. Use tabbed UI, docking UI or something like that.