在 c# windows 应用程序中查找打开的表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1108368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the open forms in c# windows application
提问by Anuya
I am using this function to close existing form and open a new form.
我正在使用此功能关闭现有表单并打开一个新表单。
If there is no exixting form, it throws error.
如果没有exixting表单,它会抛出错误。
Error :
错误 :
Target : System.Object MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)
目标:System.Object MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)
Message : Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
消息:在创建窗口句柄之前,无法在控件上调用 Invoke 或 BeginInvoke。
Stack : at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
堆栈:在 System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
SO need to check for any form open before closing the form to avoid the error. How?
所以需要在关闭表单之前检查任何打开的表单以避免错误。如何?
static public void NewMainForm(Form main, bool ClosePreviousMain)
{
if (main != null)
{
Global.ActiveForm = main.Text;
if (ClosePreviousMain & MyContext.curMain != null)
{
MyContext.curMain.FormClosed -= new FormClosedEventHandler(main_FormClosed);
//Need to check for any form active and then close the form.
MyContext.curMain.Invoke(new Action(MyContext.curMain.Dispose));
}
MyContext.curMain = main;
MyContext.curMain.FormClosed += new FormClosedEventHandler(main_FormClosed);
MyContext.curMain.ShowDialog();
}
}
采纳答案by Julien Poulin
You can use the Application.OpenFormscollection.
您可以使用Application.OpenForms集合。
回答by Teju MB
if (ApplicationFormStatus.CheckIfFormIsOpen("FormName"))
{
// It means it exists, so close the form
}
public bool CheckIfFormIsOpen(string formname)
{
//FormCollection fc = Application.OpenForms;
//foreach (Form frm in fc)
//{
// if (frm.Name == formname)
// {
// return true;
// }
//}
//return false;
bool formOpen= Application.OpenForms.Cast<Form>().Any(form => form.Name == formname);
return formOpen;
}
I have pasted the two methods one simple one and the second one is the LINQ.
我已经粘贴了两种方法,一种是简单的,另一种是 LINQ。
回答by Venugopal M
If you know the name of the form you can as well do like this :
如果您知道表单的名称,您也可以这样做:
var frm = Application.OpenForms.Cast<Form>().Where(x => x.Name == "MyForm").FirstOrDefault();
if (null != frm)
{
frm.Close();
frm = null;
}
回答by masoud Cheragee
this part of code searches for existing instance of a form if exists just shows it , and if not creates an instance of it
这部分代码搜索表单的现有实例,如果存在则显示它,如果不存在则创建它的实例
`foreach (Form form in Application.OpenForms)
{
if (form.GetType() == typeof(myMainform))
{
form.Activate();
form.Show();
this.Close();
return;
}
}
myMainform m = new myMainform();
m.Show();`