application.run

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


Answer this question

application.run

  • RalphTrent

    not sure if you're interested, but i created a very basic form collection that will give you a place to maintain them.  We used it in an app to make an MDI app, but without using any MDI forms so you can do some different styling.

    <a href="http://www.gotdotnet.com/userarea/filedetails.aspx FileName=FormsCollection.zip">Specialized Forms Collection</a>

  • sagan69

    Many thanks for your quick reply. The question is: in VB6 I created the forms and I called the object as the forms, because the declaration was equals to definition. But now when I create a form I'm only designing a type, an object. When I want to use this object I have to declare it with:

    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

    Regarding the "I pass to application.run a local form object, how could VB.NET to mantain it alive " question, it is kept alive because the form instance is assigned to the ApplicationContext.MainForm property. This keeps it from being destroyed.

    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

    OK, I undestand how VB.NET instantiates the  new forms (as object in C++) but I want to know, if I write these lines of 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

    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

    Application.Run creates an ApplicationContext class using the ApplicationContext(form) constructor (which sets the ApplicationContext.MainForm property to the form you passed into the Run method). It also creates a windows message loop to handle the displaying of your forms\controls

    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

    Even in VB6, when you created a form, you were only creating a type. When you said

    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)


  • application.run