new instant of a form

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
I have made a form2 as a templet form. I have a sub that creates the windows. Now after i opened a few of the windows. how do i refer back to a specific window thats opened?

VB.NET:
private sub newWindow()
   dim playWindow as New form2
   playWindow.Show()
   .
   .
   .
end sub
 
The first thing to note is something that a lot of people seem to want to overlook: forms are objects just like any other, so you treat them like you do any other objects.

You refer to a form just like you do any other object: via the variable that you assigned it to. Obviously, in order to do that, the variable you assigned it to must still exist, so you cannot use a local variable. If there will be an unknown number of instances then, just as you would do for an unknown number of instances of any other type, you would add them to a List and access them that way.

That said, the application also keeps track of open forms, via the Application.OpenForms collection or the equivalent My.Application.OpenForms collection. They will always contain all open forms, so you could use the OfType method to get only those of the type you want.
 
Back
Top