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

时间:2016-01-27 08:24:23

标签: c# forms winforms

 Application.OpenForms["formname"];

是否有其他方式可以访问打开的表单。我的应用程序虽然已打开但看不到此表单。我不知道为什么。

4 个答案:

答案 0 :(得分:2)

我建议您先调试代码,检查要加载的Form实际名称是什么:

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
}

然后,一旦您知道代码出了什么问题,只需执行以下操作:

Form form = Application.OpenForms[name]; //use the same name as whatever is available according to your debug

获取form

要查看有关可能错误的更多信息,请参阅Hans Passant's Post

答案 1 :(得分:1)

您必须首先实现for (i = 0; i < N; i += 2) { arr[2 * i + 0] = A[i]; arr[2 * i + 1] = A[i+1]; arr[2 * i + 2] = B[i]; arr[2 * i + 3] = B[i+1]; } 。之后你可以访问它:

Form

答案 2 :(得分:1)

获取开放表格并不是真正必要的名称。 您可以通过索引获得所需的表单:

Form frm = Application.OpenForms[0] //Will get the main form
Form frm = Application.OpenForms[1] //Will get the first child

OpenForms集合中的表单的排序方式与创建它的方式相同

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

//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);

(myClass是将保留对表单的引用的类,假设您从不同的类访问它)

(另外,看看你是否受此影响:Application.OpenForms.Count = 0 always

答案 3 :(得分:0)

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

请记住它不是实例名称,也不是形式文本:

  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";