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?
Re: Doubt about Main() method
Yes .ShowDialog will put the form on top of the other forms.
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.
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:
Public Class MyApp
<STAThread()> Public Shared Sub Main(args As String())
Application.EnableVisualStyles()
Application.Run(New frmMain())
End Sub
End Class
ShowDialog should not be used unless it really is a dialogue box.
Re: Doubt about Main() method
You could put FrmMain as starting form...
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?
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.)
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.
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.
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?
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.