UCM Posted February 19, 2003 Posted February 19, 2003 (edited) Ok guys, here's the scoop... I open a new blank vb.net project and create 2x forms, each with 1 text box and a button as well as a single module ( I know using modules isn't really the best OOP practice but I'm kinds in a hurry on this project a sin I have less than a couple weeks tobuild and test it... )...... then I add the following code: form1.vb: Public Class Form1 Inherits System.Windows.Forms.Form Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click AccessMultipleFormsInCodeWITHaModule.switchFormForm1ToForm2() End Sub End Class form2.vb: Public Class Form2 Inherits System.Windows.Forms.Form Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click AccessMultipleFormsInCodeWITHaModule.switchFormForm2ToForm1() End Sub End Class module1.vb: Module Module1 Private myForm1 As New Form1() Private myForm2 As New Form2() Public Sub Main() myForm1.Show() myForm1.Focus() End Sub Public Sub switchFormForm1ToForm2() myForm1.Hide() myForm2.Show() End Sub Public Sub switchFormForm2ToForm1() myForm2.Hide() myForm1.Show() End Sub End Module and last but not least, I set in project properties, the startup object to: Sub Main That's it... My goal here is to create from scratch 2 forms, each running with a single instance and be able to switch from one to the other... The odd thing is that whenever I run this program, form1 appears for an instant and then the program closes as though it's ended normally... The only explaination I have for this is that it is possbile that the program runs the code in sub main and then stops execution as though the program has no more code to run... I suppose 1 solution is to put all of my module code into a form3 and set form3 to be the startup object...The thing is that when I try that, then I'm back to the ol 'nullInstance' issue... Any ideas?accessmultipleformsincodewithamodule-2-19-03.zip Edited March 15, 2007 by PlausiblyDamp Quote UCM >-)
*Experts* Volte Posted February 19, 2003 *Experts* Posted February 19, 2003 In Sub Main you need to use 'Application.Run()' to start a standard Windows Message loop to keep the program alive. If you useApplication.Run()You can show forms and such normally, like you are doing; however, the program will not close itself when the last form is closed. You need to do it manually. The alternative is to do it like this:Application.Run(myForm1)This will start the program with myForm1 as the main form. The program will then shut itself down when myForm1 is closed (it is the main form now). If you need to be able to have myForm1 closed at any given time while another form is showing, then the first method is the best. If myForm1 is going to be showing at all times regardless of the other forms' visibility, the second method is best. Quote
UCM Posted February 19, 2003 Author Posted February 19, 2003 (edited) Sweeeeeeeet!!! alllrighhht!!! That did the trick Perfectly!! Thank you very much and just in case anyone else is reading this thread and would like to see how VolteFace made my example work, here it is, complete and fully operational... Module Module1 Private myForm1 As New Form1() Private myForm2 As New Form2() Public Sub Main() Application.Run(myForm1)' <--ADD this line 'myForm1.Show() <--REM this line out 'myForm1.Focus() <--REM this line out End Sub Public Sub switchFormForm1ToForm2() myForm1.Hide() myForm2.Show() End Sub Public Sub switchFormForm2ToForm1() myForm2.Hide() myForm1.Show() End Sub End Module accessmultipleformsincodewithamodule-2-19-03-ucm-volteface.zip Edited March 15, 2007 by PlausiblyDamp Quote UCM >-)
UCM Posted February 19, 2003 Author Posted February 19, 2003 Forgot to mention... If anyone out there uses this example then I'm sure you will want an idea on how to close the program when the user is done... The thing to remember here is that if you just hit the 'X' on a form then the program will still be running so one way to close the program is to use the End command in your code when it is time for the entire program to end... Quote UCM >-)
*Gurus* divil Posted February 20, 2003 *Gurus* Posted February 20, 2003 Using the End command is bad practice. If you need to terminate your message pump (which is what you need to do) stick Application.Exit() in the Closed event of your form. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
UCM Posted February 22, 2003 Author Posted February 22, 2003 (edited) Thanx, Divil... Ok guys, the best way to get this to close is to modify the Module1.vb like: Module Module1 Private myForm1 As New Form1() Private myForm2 As New Form2() Public Sub Main() Application.Run(myForm1)' <--ADD this line 'myForm1.Show() <--REM this line out 'myForm1.Focus() <--REM this line out End Sub Public Sub switchFormForm1ToForm2() myForm1.Hide() myForm2.Show() End Sub Public Sub switchFormForm2ToForm1() myForm2.Hide() myForm1.Show() End Sub Public Sub CloseMyApplication() AccessMultipleFormsInCodeWITHaModule.CloseMyApplication() End Sub End Module and whenever ( and anywhere at all in your application ) you want to close your entire application, use: AccessMultipleFormsInCodeWITHaModule.CloseMyApplication() I have uploaded the completed version of this endevour in this post... This works perfectly for all intents and purposes, however, you need to use the module with sub Main() for it to work properly... I am still working on a moduleless version of this by using a form3 as the main startup object instead of a module or sub Main() but thus far, no success...When I succed ( or if you guys know a way to modify this completed example to work with a form3 and no module ) then I will finish an example and upload it in this thread as well... The major advantage to having a form3 instead of a module is that you can add things like notifyicons and set up all of the advantages of using an object over a module...Itsjust a matter of getting a moduleless version of this example to work, lol...accessmultipleformsincodewithamodule.zip Edited March 15, 2007 by PlausiblyDamp Quote UCM >-)
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.