Find an instance of a form from another class

I have a main form with a list of data inside a listBox. When I click the button, I open a new form to create a new data object (the main form is inactive in the background), when new data is transferred to the listobox inside the main form, it must be filled with this new object.

I thought the following:

  • When Form2 was submitted, I thought to find an instance of MainForm and kill this instance, after which it should be easy, load the list of data from db again and display it in the list.

Question:

If Form1 is created and at some event Form2 is created using showDialog, so Form1 is inactive until the data is presented, how can I find an instance of Form1 before closing Form2?

So how to find an instance of class Form1 from class Form2?

thanks

+4
source share
2 answers

You can get a link to any of the currently open application forms using the Application.OpenForms property. Forms in this FormCollection can be obtained by index as follows:

 Form form1 = Application.OpenForms[0]; 

or by the Name property, for example:

 Form form1 = Application.OpenForms["Form1"]; 

Hope this helps.

+29
source

if you call

 Form1.ShowDialog(this) 

then you can get a link to the calling form using

 this.Owner.Name 

in the second form (form 2 in your case)

see http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog.aspx

0
source

Source: https://habr.com/ru/post/1490148/


All Articles