Results 1 to 11 of 11

Thread: Doubt about Main() method

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Doubt about Main() method

    I'm new at VB.Net, and I haven't had too much time to work on it, so I'm still trying to learn several things.

    In VB6, when using the Main sub there was no problem with doing:

    Load frmMain
    frmMain.Show

    However, using the Show() method in .Net causes the form to appear and disappear, and I have to use ShowDialog() instead. Am I doing it correctly?

  2. #2
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Doubt about Main() method

    Yes .ShowDialog will put the form on top of the other forms.
    "The dark side clouds everything. Impossible to see the future is."

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: Doubt about Main() method

    Just in case I haven't been too clear, when I said the form appears and disappears using Show() I meant that once the forms appears the program is finished afterwards.

    Quote Originally Posted by Asgorath
    Yes .ShowDialog will put the form on top of the other forms.
    Yes, I've seen it's the method to show a form as modal as well.

    Thanks for your reply.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Doubt about Main() method

    It disappears because the only thing to keep an application running is a message loop. To start one of those you need to use Application.Run.

    VB.NET Code:
    1. Public Class MyApp
    2.   <STAThread()> Public Shared Sub Main(args As String())
    3.     Application.EnableVisualStyles()
    4.     Application.Run(New frmMain())
    5.   End Sub
    6. End Class

    ShowDialog should not be used unless it really is a dialogue box.

  5. #5
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Doubt about Main() method

    You could put FrmMain as starting form...
    "The dark side clouds everything. Impossible to see the future is."

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: Doubt about Main() method

    Quote Originally Posted by penagate
    Application.Run(New frmMain())
    I see, thanks for pointing out that.

    This made me wonder another thing, should I use Application.Exit() instead of Me.Close() when closing the main form in this situation? Also, I guess Application.Exit() dispose every other form in memory, and I shouldn't worry about iterating through all of them? or should I? and, would there be any difference between a MDI application and SDI with multiple forms?

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Doubt about Main() method

    Application.Exit will, yes, close every form. If your application tends to spawn windows all over the place and you want to say goodbye to the whole lot at once, this is a quick way to do it. If you have one window open, calling its Close method will end the application too. If this window is an MDI form, all of its child windows will be closed, so it is equivalent to calling Application.Exit. However, with an MDI application, I would use Close (not that I can think of any particular reason why.)

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: Doubt about Main() method

    Quote Originally Posted by penagate
    Application.Exit will, yes, close every form. If your application tends to spawn windows all over the place and you want to say goodbye to the whole lot at once, this is a quick way to do it. If you have one window open, calling its Close method will end the application too. If this window is an MDI form, all of its child windows will be closed, so it is equivalent to calling Application.Exit. However, with an MDI application, I would use Close (not that I can think of any particular reason why.)
    But is Application.Exit() anything like End? or does it successfully release every form? I'm very used to iterate through any active form and unload and release it, so I guess I'll keep doing it, but if I can sometimes save that time of writing code the better.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Doubt about Main() method

    Quote Originally Posted by Neverbirth
    iterate through any active form and unload and release it
    That's exactly what Application.Exit does.
    Quote Originally Posted by [url=http://msdn2.microsoft.com/en-us/library/ms157894.aspx]Application.Exit Method (System.Windows.Forms)[/url]
    The Exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return.

  10. #10
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    Re: Doubt about Main() method

    Quote Originally Posted by Asgorath
    You could put FrmMain as starting form...
    Or, if you're using VS 2005, you could enable the Application Framework and use the Application Events, like StartUp, Shutdown, and UnhandledException to better manage your WinForms app. But why should you want to do things the easy way?

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: Doubt about Main() method

    Quote Originally Posted by bgmacaw
    Or, if you're using VS 2005, you could enable the Application Framework and use the Application Events, like StartUp, Shutdown, and UnhandledException to better manage your WinForms app. But why should you want to do things the easy way?
    I didn't know about it, I read your post yesterday, and found the Application Events to be interesting.

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width