3
 Application.OpenForms["formname"];

Is there any other way to access an open form. My application doesn't see this form although it is opened. I dont know why.

nrtn93
  • 101
  • 2
  • 10

4 Answers4

2

I recommend you to firstly debug your code to check what is the Form actual name which you want to load:

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
}

And then, once you know what is wrong with your code, simply do:

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

To get your form.

To check more on the possible bugs, See Hans Passant's Post

Community
  • 1
  • 1
Ian
  • 30,182
  • 19
  • 69
  • 107
2

Isn't really necessary a name to get an open form. You can get the form you want by index:

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

Forms in the OpenForms collection are ordered in the same way that you create it

Otherwise, an alternative is to save a reference to the form and then accessing it by this reference.

//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 is the class that will hold your reference to the form, supposing you are accessing it from different classes)

(Also, see if you are affected by this: Application.OpenForms.Count = 0 always)

Community
  • 1
  • 1
fruggiero
  • 943
  • 12
  • 20
  • *Forms in the OpenForms collection are ordered in the same way that you create it* Is this documented? – nawfal May 12 '20 at 04:43
  • 1
    @nawfal as we can see from the source: https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Form.cs,4833 a form is added to that collection on the form OnLoad event (with OpenFormsInternalAdd). So the order should be guaranteed as long as we show forms in a sequential manner. – fruggiero May 12 '20 at 08:26
1

You have to instanciate first a Form. after that you have access to it:

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

// access to form by formname.yourproperty
M. Schena
  • 2,039
  • 1
  • 21
  • 29
  • What does it mean? Application.Run(formname)? – nrtn93 Jan 27 '16 at 11:30
  • 1
    Begins running a standard application message loop on the current thread, and makes the specified form visible: https://msdn.microsoft.com/en-us/library/ms157902(v=vs.110).aspx – M. Schena Jan 27 '16 at 11:46
0

To access the form using this property your form must have a Name.

Remember its not the instance name nor the the form Text:

  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"

Sample:

frm_myform form1 = new frm_myform(); 
frm_myform.Name = "form1";
Shaharyar
  • 12,254
  • 4
  • 46
  • 66