Showing Form in Sub Main

G

Guest

I have a Sub Main from which I show a Form:

Public MainForm as New Form1
<STAThread()> Public Sub Main()
MainForm.Show()
Application.Run()
End Sub

The MainForm (Form1) shows ok and runs ok until I show a Form2 from Form1.
This works Ok but when I close Form2, I get an error at the above
"Application(Run)" line stating an "External Component has thrown an
exception" How can I continue executing code and accepting events in Form1
after I close Form2
 
G

Guest

I cannot see why you are wanting to run from Sub Main

Try using Application.Run(MainForm)


What are you actually trying to do? Create a splash screen or something?
 
D

Dustin Brisebois via DotNetMonster.com

Cheating would be

try

catch ex as exception
finally

but leave the blank after the Catch Ex - then the error doesn't do
anything. However this may cause a slight performance problem.

Where is Sub Main located in the Application? I'd ditch the Application.Run
() - since it will run anyway right?

or below ---

Public MainForm as New Form1

Protected Sub Main_Load (byval sender as system.object, byval e as
system.eventargs) handles mybase.load
MainForm.Show()
End Sub


this should fire as soon as the main window loads - you can repeat the load
anywhere (like in Form1)
 
C

Chris Dunaway

A form by itself cannot run. There must be a message pump for the app
to continue to run, that's what Application.Run.

I doubt that your problem is caused by the Application.Run. When you
get the exception, what does the stack trace show? Put a Try Catch
block around your code and catch the exception and then print out the
stack trace. That should yeild some additional information:

Public MainForm as New Form1
<STAThread()> Public Sub Main()
Try
MainForm.Show()
Application.Run()
Catch ex As Exception
MsgBox(ex.StackTrace)
End Try
End Sub
 
G

Guest

I have tried that and no I'm not into splash screens. I just want to show a
second Form using .ShowDialog from the first for the user to chose some
options then click OK. However, when the Form I showed using .ShowDialog
closes, the program crashes giving me the error on the Application.Run or the
Application.Run(MainForm) line.
 
G

Guest

Thanks. I"ll work with it some more. The Sub Main is in a module set as
the application start-up sub. If I bypass the Sub Main and just set my Form1
as the start-up form, it runs fine and when my Form2 dialog closes, it
continues executing in Form1 at the statement after the Form2.ShowDialog
statement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top