Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Showing forms from Sub Main

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
How do I show a form from sub main module?

I have this in module main

Public frmMain as New FormMain


Public Sub Main()

'This does not. It shows the form then ends the program
frmMain.Show()


'This works
frmMain.ShowDialog()

End Sub


How can I show a form non model and still run my program?
 
If your Main() sub exits, your program is going to end. That's by definition. You can show the form non-modally, then continue to do other things -- including, perhaps, looping and checking to see if all of the forms you have displayed have been closed, exiting if they have been.

 
VB6 never did it this way??? How do you set up all of your globals then if you don't start in sub main?

Am I wrong from the start?

All I have in the sub is:

Public Sub Main()

'This works
frmMain.ShowDialog()

End Sub


But I have lots of vars declared in the top of the module.
 
frmMain.Activeform.Show() did not work. It gave me an error.

Help please.
 
You don't have to start in Sub Main in order to use global variables in your module.
 
Ok, I have it working but now I have both forms in memory, and that's not what I wanted. When I close the app, they will still be out there, right? Sucking up memory?


Module1
Public frmMain As New FormMain
Public frmNext As New FormNext


frmMain's button click
'I wanted to close this form
Me.Visible=False
frmNext.show


frmNext's button click
'I wanted to close this form
Me.Visible=False
frmMain.show






 
As a second side effect to my last example: The application will not close now, since there is always a form hidden in memory.
 
I just found this article:

It explains a lot. Now I want to go back to VB6. It was so much easer then. LOL

I need to treat forms like classes, with the main form always in memory and the forms it calls not.

What if Form1 calls Form2, then Form2 calls Form3? Now I have 2 forms to worry about disposing of?

I think I'm sick. P-)
 
Now it's working nice, and only one form in memory at a time:


Module1
Public frmMain As New FormMain
Public frmNext As New FormNext


frmMain's button click to show frmNext
Me.Hide()
frmNext.ShowDialog()
'The code stops here until frmNext is closed.
Me.Show()


frmNext's button click to show frmMain again
Me.Close()


Still not sure this is the right approach, but hopefully this will keep anyone else from yanking out their hair.::)
 
Bigfoot,

As far as your "second side effect", try adding this:

[tt] Private Sub process_exit_request(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
End 'ends the application; if this not done, Main form (and hence the application, too) persist
End Sub[/tt]

It does seem ridiculous, but I end up adding this to each and every class - and it helps. It's just stating the obvious ("when user tries to close, let him") but it works...

:)
Lazer
 
Another side effect. Since I am not calling my form as modal, i.e. ShowDialog. When I write something to a textbox on the form from my sub, it does not show up.

I even tried refreshing the form. It's not until I set it back to calling it ShowDialog, that the text shows up in the status bar.

Drats, foiled again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top