Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i know there's a simple answer to this

Status
Not open for further replies.

medicenpringles

Programmer
Aug 7, 2005
39
US
i have a project w/ several forms. i know you can open a project using "sub main", and that's what i try to do

but for some reason i can't get the sub main to load my main form. here's my code:

Code:
Module Globals
Public MainForm as Form = New frmMain
Sub Main()
   MainForm.Show()
End Sub
End Module

Main Language: Visual Basic .NET
Development Environment: Visual Studio .NET 2003
 
Module Globals
Public MainForm as frmMain
Sub Main()
MainForm = New FormMain
Application.Run (FormMain)
End Sub
End Module

should work assuming Sub Main is set in Project Properties as startup object.


Hope this helps
 
I may be wrong about this, but I think that for this to work, you have to use MainForm.ShowDialog. The thing is that ShowDialog is modal -- in some situations this could be a problem.

I had the same problem yesterday. It has something to do with being single threaded. Sorry I don't understand more about this.

Hope this helps.
 
CodeHorse, you are correct about MainForm.Show terminating as soon as it starts because there is essentially nothing to keep it open and that MainForm.ShowDialog will keep the form open. However as far as I am aware, various other "things" are setup behind the scenes when a program starts and I've always understood Application.Run to be the better and safer way of starting a program using Sub Main.

If the startup object is a form, I believe that behind the scenes VB creates a Sub Main which includes the equivalent of Application.Run.


Hope this helps.
 
Code:
Sub Main()
   MainForm = New FormMain
   Application.Run (FormMain)
End Sub
I have a form that I use in this way, and it works non-modally.

Bob
 
Thats right.

Using this method a form doesn't need to be modal. Application.Run starts the program's message queue, which will keep going until it receives the "exit program" message (there is a specified constant for this, but off hand I don't know what it is).

Using MainForm.ShowDialog opens a dialog box (modal form) - which by its very nature will remain on screen until dismissed. Whilst I'm not saying that this method of starting a program is wrong, and I'm not sure what if any the consequences may be, I've always been led to believe that the "correct" way is to use Application.Run.
 
Not to quibble, but shouldn't this be:
Code:
Module Globals
Public MainForm as frmMain
Sub Main()
   MainForm = New FormMain
   Application.Run(MainForm)
End Sub
End Module
instead of
Code:
Module Globals
Public MainForm as frmMain
Sub Main()
   MainForm = New FormMain
   Application.Run(FormMain)
End Sub
End Module

Have a great day!

j2consulting@yahoo.com
 
Code:
'My first "attempt"
Module Globals
Public MainForm as frmMain
Sub Main()
   MainForm = New FormMain
   Application.Run (FormMain)
End Sub
End Module

Code:
'SBendBuckeye's correction
Module Globals
Public MainForm as frmMain
Sub Main()
   MainForm = New FormMain
   Application.Run(MainForm)
End Sub
End Module

Code:
'My corrected correction
Module Globals
Public MainForm as frmMain
Sub Main()
   MainForm = New [b]frm[/b]Main
   Application.Run(MainForm)
End Sub
End Module

of course if you don't need the global variable then
Code:
Module Globals
Sub Main()
   Application.Run(New frmMain)
End Sub
End Module
should work

[smile]
 
Nice work EandF! It must be Friday. As an aside, did you happen to see my thread re: BindingMangager vs CurrencyManager? You are one of the experts and this one has me pretty well stumped. Thanks!

Have a great day!

j2consulting@yahoo.com
 
You are one of the experts and this one has me pretty well stumped. [blush] Unfortunately, [this is not for public consumption] I use the W's [/this is not for public consumption] to do the data nitty-gritty. Although I must look into to taking control of that side of things myself.
 
I hope you don't mean the WIZARDS. I think that you should loose at least a hundred stars for that and get banned for life from any machine that has a processor in it.

and here we have zorro again.

[censored][censored][censored][censored][censored][censored][censored][censored][censored][censored]

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top