I need to update a ListBox of a Form2 created dynamically. Let’s say that in the event where I have to update this ListBox (in Form1 of course) I don’t have a reference of this Form2 so I can’t call the UpdateList method (and no, I can’t make it static).
I don’t even know if there is a Form2 opened, it could be or not.
What do you suggest?
Is there a way to loop through all the open istances of Form2?
Code Sample:
//Form1
public void event()
{
//UPDATE FORM2 LISTBOX
}
//SOMEWHERE IN FORM1
Form2 runTime = new Form2();
//Form2
public void UpdateList()
{
//UPDATE LISTBOX
}
I’m not sure what exactly do you want to implement. But it seems to me that you can just iterate through the collection of opened forms:
This line will give you the IEnumerable of all open Form2 instances in your application. You might want to use your own string representation (not the form caption used in the snippet above)