CREATING MODULES

I AM TRYING TO USE A MODULE TO CALL SEVERAL FORMS IN VB2005 EXPRESS.

THE PROGRAM LOOKS LIKE THIS:

MODULE MODULE1
DIM form1As New Form1
DIM form2As New Form2

Sub Main()
MsgBox("This is module")
form1.Show()' trying to call form1
form2.Show()
End Sub
End Module

Public Class Form1

Private SubButton1_click(.........) Handles
Button1.Click
Label1.text ="Hello World"'print if
program gets here'
End Sub

Private SubForm1_Load(By value .........)
Handles Mybase.Load
MsgBox("This is Form1")
End Sub
End Class

Public Class Form2

Private SubButton1_click(.........) Handles
Button1.Click
Label1.text ="Hello World"'print if
program gets here'
End Sub

Private SubForm2_Load(By value .........)
Handles Mybase.Load
MsgBox("This is Form2")
End Sub
End Class

The program works like this:

The message"This is module"was used to let me know that the program started in the module.
When the form1.Show() command is executed inSub Main of the module ; control transfers to form1. The message"This is Form1"lets me know than the form1_Load procedure worked.
The problem at this point is that the program returns back to the module.I want the program to stay in form1 until I click themouse button on Button1_Click.
Now it gets wierd to me.
When form2.show() is executed; control is passed to form2 and the messagebox"This is form2" is executed in the form2_load handler.
At this point the odd thing is that the form1 visual window appears on the screen along with the messagebox control. Pressing ok in messagebox returns control to the module.Again I want the program to stay in form2.

[3199 byte] By [NUMBSCULL] at [2007-12-16]
# 1
You need to call Application.Run(form1) in your Main method instead of form1.Show(). Show returns as soon as the form is shown, while Run returns when the form closes.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
Try to use form1.ShowDialog() instead of .Show()
This will stop execution until the form is closed.

Dustin.

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Dustin_H wrote:
Try to use form1.ShowDialog() instead of .Show()
This will stop execution until the form is closed.

Dustin.

Unless your form actually is a dialogue box that you want to return a DialogResult, using ShowDialog is hack. It is standard practice to call Application.Run from a Main method. Having said that, unless you want to do something else in your Main method you might as well just make the form the starup object for the application. A typical reason to use a Main method is to enable visual styles.

Public Sub Main()
Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(New Form1)
End Sub


jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Hello jm
I tried to use Application.Run(form1) in vb2005 express and i got the message that "Application is not declared" in the debugger.
Any idea of a remedy for this?

NUMBSCULL at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
hello jm
I used form1.ShowDialog() and the program is executing like i want it to.The reason for the use of the module is to declare public variables
which can be used by all forms in my application. This was your idea several weeks ago when i started using the forum. i was using the small program just to test and make sure that everything will work in vb. Do you declare the public variables in the Sub Main method?
NUMBSCULL at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
NUMBSCULL wrote:
hello jm
I used form1.ShowDialog() and the program is executing like i want it to.The reason for the use of the module is to declare public variables
which can be used by all forms in my application. This was your idea several weeks ago when i started using the forum. i was using the small program just to test and make sure that everything will work in vb. Do you declare the public variables in the Sub Main method?

You just declare the variables with the "Public" keyword at the top of the module. No necessity for the main() method, unless you want to do extra stuff.
bud1024 at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...