URGENTLY needs help! Form loads and immediately unloads?!

J

John Doe

Hi,

Hopefully someone can help me on this one.
I normally start all my VB apps from a Sub Main, which I place in a code
module. This Sub then instantiates a form, i.e

Public Sub Main()
Dim MyMainForm as new frmMain

MyMainForm.Show()

End Sub

NOW, (dunno when exactly it stopped working) this code would instatiate the
form for a split second, the form (instead of remaining active) shows, and
then immediately unloads, sub main finishes, and the program exit. I tested
this on a colleague's computer as well, and it does the exact same thing.
Can anyone tell me how to fix this?

I'm running Visual Studio.NET 2003 Professional
IDE version 7.1.3088
..NET framework version 1.1.4322 SP1
on a winXP 2002 SP1 installation.

Any ideas?

Johan van Niekerk
Lecturer: Business Information Systems
Faculty of Engineering
Nelson Mandela Metropolitan University
 
L

Larry Lard

John said:
Hi,

Hopefully someone can help me on this one.
I normally start all my VB apps from a Sub Main, which I place in a code
module. This Sub then instantiates a form, i.e

Public Sub Main()
Dim MyMainForm as new frmMain

MyMainForm.Show()

End Sub

NOW, (dunno when exactly it stopped working) this code would instatiate the
form for a split second, the form (instead of remaining active) shows, and
then immediately unloads, sub main finishes, and the program exit. I tested
this on a colleague's computer as well, and it does the exact same thing.
Can anyone tell me how to fix this?

This is a key 'gotcha' in the move from vb6 to vb.net. A VB6
application continues to run so long as any objects that it created
still exist. A VB.Net application will end as soon as its startup
object ends. So if you set a VB.Net application to have Sub Main as its
startup object, as soon as that Sub ends, the app will exit! Which is
probably not what you want.

Two approaches:

- Show your form *modally* from Sub Main using form.ShowDialog()

- Specify a form as the Startup Object, and do whatever initialization
you need to do in that form's Load event procedure.
 
C

Charles Law

Hi John

In addition to Larry's answer, you can use Application.Run in your sub Main
to start your main form.

HTH

Charles
 
J

John Doe

This is a key 'gotcha' in the move from vb6 to vb.net. A VB6
application continues to run so long as any objects that it created
still exist. A VB.Net application will end as soon as its startup
object ends. So if you set a VB.Net application to have Sub Main as its
startup object, as soon as that Sub ends, the app will exit! Which is
probably not what you want.

Thanks, just another question though:

Surely the form IS an object, and SHOULD persist until the user unloads it?
In other words, VB should be event driven, once the form is in memory, its
should hang around until an event occurs that removes it from memory. Or do
I misinterpret how its working?

Two approaches:

- Show your form *modally* from Sub Main using form.ShowDialog()

- Specify a form as the Startup Object, and do whatever initialization
you need to do in that form's Load event procedure.

I'll try those, thanks :)
 
P

Peter van der Goes

John Doe said:
Thanks, just another question though:

Surely the form IS an object, and SHOULD persist until the user unloads
it?
In other words, VB should be event driven, once the form is in memory, its
should hang around until an event occurs that removes it from memory. Or
do
I misinterpret how its working?
VB.NET is event driven, but also object oriented.
When you instantiate your form class,

"Dim MyMainForm as new frmMain"

within the Main() method, the rules of scope apply unless you employ special
measures (like displaying the form modally, which requires that it be
dismissed).
A locally (within a code block) created object goes out of scope when you
exit the block (in this case, Main()) where the object was created.
 
J

John Doe

Peter van der Goes said:
VB.NET is event driven, but also object oriented.
When you instantiate your form class,

"Dim MyMainForm as new frmMain"

within the Main() method, the rules of scope apply unless you employ special
measures (like displaying the form modally, which requires that it be
dismissed).
A locally (within a code block) created object goes out of scope when you
exit the block (in this case, Main()) where the object was created.

That's the first thing I assumed, so I tried putting the dimension statement
in the general declarations
section of the module, and only called the form.show() method from the Sub
Main(). The form (which should still be in scope) STILL unloads immediately.
 
H

Herfried K. Wagner [MVP]

John,

John Doe said:
I normally start all my VB apps from a Sub Main, which I place in a code
module. This Sub then instantiates a form, i.e

Public Sub Main()
Dim MyMainForm as new frmMain

MyMainForm.Show()

Your application is lacking a message pump and thus the application will
terminate. Set the form as startup object in the project properties or add
this code to a module and select 'Sub Main' as startup object:

\\\
Public Module Program
Public Sub Main()
Application.Run(New MainForm())
End Sub
End Module
///
 
Top