C# 有没有其他方法可以访问C中的开放表单?

C# 有没有其他方法可以访问C中的开放表单?,c#,forms,winforms,C#,Forms,Winforms,是否有其他方法访问打开的表单。我的应用程序没有看到此表单,尽管它已打开。我不知道为什么。要使用此属性访问表单,您的表单必须具有 记住它不是实例名称,也不是表单文本: Application.OpenForms["formname"]; 样本: Form1 f1 = new Form1(); //not "f1" is the "Name" f1.Text = "it is title of the form"; //neither "Text" is the "Name"

是否有其他方法访问打开的表单。我的应用程序没有看到此表单,尽管它已打开。我不知道为什么。

要使用此属性访问表单,您的表单必须具有

记住它不是实例名称,也不是表单文本:

 Application.OpenForms["formname"];
样本:

  Form1 f1 = new Form1();    //not "f1" is the "Name"  
  f1.Text = "it is title of the form"; //neither "Text" is the "Name"
  f1.Name= "its the name"; //it is the "Name"

你必须先实例化一个表单。之后,您可以访问它:

frm_myform form1 = new frm_myform(); 
frm_myform.Name = "form1";

我建议您首先调试代码,以检查要加载的表单实际名称:

Form1 formname = new Form1();
Application.Run(formname);

// access to form by formname.yourproperty
然后,一旦您知道代码有什么问题,只需执行以下操作:

foreach (Form form in Application.OpenForms) {
    string name = form.Name; //check out this name!!
    //print, or anything else will do, you only want to get the name
    //note that you should be able to get any form as long as you get its name correct
}
去拿你的表格


要查看更多可能的bug,请参见

获取打开的表单实际上不需要名称。 您可以通过索引获取所需的表单:

Form form = Application.OpenForms[name]; //use the same name as whatever is available according to your debug
OpenForms集合中的表单的排序方式与创建表单的方式相同

否则,另一种方法是保存对表单的引用,然后通过该引用访问表单

Form frm = Application.OpenForms[0] //Will get the main form
Form frm = Application.OpenForms[1] //Will get the first child
myClass是保存表单引用的类,假设您从不同的类访问表单


另外,请查看是否受此影响:

此formname是打开窗体实例的名称还是打开窗体类的名称?也许你也可以在这里发布你的代码,特别是与FormName相关的代码。你设置了表单的名称吗?OpenForms集合包含什么?看看所有打开的表单:MessageBox.ShowString.JoinEnvironment.NewLine,Application.OpenForms.OfType.Selectform=>form.name;你能找到所需的表单吗?不要忘记你创建的表单对象。我知道,这不是文本。我使用表单名称,但Application.openForms既不返回null,也不返回其他内容。frm_myform form1=new frm_myform;然后可以通过form1变量访问表单,对吗?是的。我使用form1变量访问它。@nrtn93我要求提供完整的代码,因为在这一行中您没有设置窗体的Name属性。这就是我提到的,这是什么意思?Application.Runformname?开始在当前线程上运行标准应用程序消息循环,并使指定的表单可见:OpenForms集合中的表单的排序方式与您创建表单的方式相同。是否有此文档?@nawfal,正如我们从源代码中看到的那样:在使用OpenFormsInternalad的表单OnLoad事件中,表单被添加到该集合中。因此,只要我们以顺序的方式显示表单,就应该保证顺序。
//Where you want to save the reference:
Form theForm;
//Where you create the form:
myClass.theForm = new MyForm();
//Where you want to get that form:
MessageBox.Show(myClass.theForm.Caption);