Hello, koorb:
Your problem arises because all Windows Forms programs must have a message loop.
You can create the message loop with the application.run method. Depending on how you call application.run, you should exit the message loop in a different way:
· When calling application.run() the message loop keeps working until you call application.exitthread. Until then, it keeps running even if you close all the forms. When you call application.exitthread, all forms depending on that message loop are closed.
· When calling application.run(form) the message loop ends when you close the form.
· When calling application.run(applicationcontext) the message loop ends when you close the applicationcontext.mainform. Note that you can change the applicationcontext.mainform to a second form, then close (or leave visible) the first one; this way the message loop ends when you close the second form.
I haven't been able to understand completely the behavior of application.exitthread when I use application.run(form) or application.run(applicationcontext). It seems to end the message loop, close the form and then skip the two following message loops. It seems that a modal form (like messagebox or form.showdialog) creates a message loop if there isn't one running.
I have ilustrated all with a sample that you can compile with the following command:
\WINDOWS\Microsoft.NET\Framework\v1.1.4322\vbc.exe /target:winexe /imports:system.windows.forms,microsoft.visualbasic,system.collections /r:system.windows.forms.dll,system.dll,System.Drawing.dll,Microsoft.VisualBasic.dll /m:startmodule appexit.vb
Note the following:
· Using form.close in all the forms of the second loop of my sample will make the program keep working without user interface. In that case, use the task manager to kill the process.
· For modeless forms, pressing the form's close button ("x") is the same that calling form.close.
· The form.closing event is not fired if the form is not explicitly closed, for example when calling application.exitthread.
'********************** start of appexit.vb file **********************
imports system.windows.forms
module StartModule
friend ac as applicationcontext
public sub Main()
dim a,b,c as form
msgbox("Program starts.")
a=new form1
a.text="First message loop. Use the close button."
application.run(a)
msgbox("First message loop ended.")
a=new form1:b=new form1:c=new form2
a.text="Second message loop. Use ExitThread on any form."
a.show:b.show:c.show
application.run() 'Use ExitThread. If you close all the forms, the message loop keeps running and the program does not end.
'Note: When using application.run() you can use the form.closing event to call application.exitthread.
msgbox("Second message loop ended.")
a=new form1
a.text="Third message loop. Use the close button or change the form."
ac=new applicationcontext(a)
application.run(ac)
msgbox("Program ends.")
end sub
end module
class Form1
inherits form
private withevents b1 as button
private withevents b2 as button
private withevents b3 as button
private withevents b4 as button
sub new
mybase.new
me.text="This is Form1"
b1=new button
b1.text="Open another"
b1.visible=false
b2=new button
b2.text="ExitThread"
b2.location=new system.drawing.point(0,25)
b3=new button
b3.text="Close"
b3.location=new system.drawing.point(0,50)
b4=new button
b4.text="Change form"
b4.location=new system.drawing.point(0,75)
me.controls.addrange(new control(){b1,b2,b3,b4})
end sub
private sub b1_click(p1 as object, p2 as system.eventargs) handles b1.click
call (new form1).show
end sub
private sub b2_click(p1 as object, p2 as system.eventargs) handles b2.click
application.exitthread
end sub
private sub b3_click(p1 as object, p2 as system.eventargs) handles b3.click
me.close
end sub
private sub b4_click(p1 as object, p2 as system.eventargs) handles b4.click
if not ac is nothing then
ac.mainform=new form2
ac.mainform.show 'The new form is not showed automatically.
me.close '«me» is not related now whith the applicationcontext object.
end if
end sub
end class
class form2
inherits form 'Empty form.
end class
'********************** end of appexit.vb file **********************
Regards.
"koorb" <
[email protected]> escribió en el mensaje | I am starting a program from a module with the Sub main procedure and
| I want it to display two forms for the program's interface, but when I
| run the program both forms just open and then program closes.
|
| Dim FORM1 As New Form1
| Dim FORM2 As New form2
|
| Sub main()
| FORM1.Show()
| FORM2.Show()
| End Sub
|
| I am assuming I need something after the second .Show(), to stop the
| program from ending, but I have no idea what. I know other people have
| used a dialogue to stop the procedure running through, but I want to
| give the user access to both forms. Can anyone help?