Every time, I run this, it just ends suddenly with no err??

UCM

Centurion
Joined
Jan 1, 2003
Messages
135
Location
Colorado, usa
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:
Visual Basic:
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:
Visual Basic:
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:
Visual Basic:
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?
 

Attachments

Last edited by a moderator:
In Sub Main you need to use 'Application.Run()' to start a standard
Windows Message loop to keep the program alive. If you use
Visual Basic:
Application.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:
Visual Basic:
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.
 
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...


Visual Basic:
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
 

Attachments

Last edited by a moderator:
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
Visual Basic:
End
command in your code when it is time for the entire program to end...
 
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.
 
Thanx, Divil... Ok guys, the best way to get this to close is to modify the Module1.vb like:

Visual Basic:
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:
Visual Basic:
    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...
 

Attachments

Last edited by a moderator:
Back
Top