I can't understand what does Application.Run method
really do. It load a form, but how can I reference to
this form after that If I create a new instance of the
form in main sub and then I pass it to Application.Run, I
have the form loaded but the object created in main sub
is already been destroyed...
Many thanks
Cold

application.run
RalphTrent
<a href="http://www.gotdotnet.com/userarea/filedetails.aspx FileName=FormsCollection.zip">Specialized Forms Collection</a>
sagan69
dim x=new object(...)
Now I have the object x, and I can access to its methods and properties easily. But, if I load my form with application.run, where is the object declared How can I have access to it
Second question...
If I write this code
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x As New Form3()
x.Show()
End Sub
the Form3 is displayed when I press the Button2 button. But, I can't understand...I create a locale object x, x is destroyed when the event procedure finishes but the form isn't destroyed, it is visible yet! Why could the form be alive
Many thanks
Cold
baldrick98007
In the case of your button2 click code, Windows forms does some special work in the Show call to ensure that the form is not garaged collected, by adding a special reference to it. Of course, if you don't keep a reference to it, then there isn't any easy way to get a reference to it later in your code.
Currently there isn't any "forms collection" thing like what VB 6 has.
-mark
dmkp231
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x As New Form3()
x.Show()
End Sub
why my new form (x) is shown also after end sub....it would have been destroyed!!! And if I pass to application.run a local form object, how could VB.NET to mantain it alive
Cold
hellomoin
About getting a reference to the form, you can do the following in the main function
[C#]
static void Main()
{
using(Form1 f = new Form1()) {
Application.Run(f);
}
}
[VB]
Shared Sub Main()
Dim f As Form1
Try
f = New Form1()
Application.Run(f)
Finally
f.Dispose()
End Try
End Sub 'Main
But I don't know if that is what you are asking about. Where do you need to get the reference at What are you trying to do You can also create your own class that derives from ApplicationContext, and use the Application.Run(ApplicationContext) overload. This will allow you to manage the lifetime of the "main form" yourself.
Let me know if this helps.
-mark
Beverley
YourForm.Show
VB6 did some fancy footwork under the covers, creating a global variable with the same name as your class, instantiating it, adding it to the Forms collection, and then showing the form. Windows Forms don't do this for you. You have to instantiate and show the form yourself, creating an instance of the class. There's really nothing different--you have just to do the work yourself (except the missing Forms collection, although you can create this yourself, too).
If you use Application.Run, you must declare the variable yourself, then pass it to Application.Run, as in:
' In a class, outside a procedure:
Private YourFormInstance As New YourFormClass
' Then, in your Sub Main:
Application.Run(YourFormInstance)