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!

Hiding a form initialized by Sub Main() 1

Status
Not open for further replies.

JFoushee

Programmer
Oct 23, 2000
200
US
Hi. I have a "Sub Main()" that does some work before showing a form
Code:
Module modMain
    Sub Main()
        'some work here

        Dim myfrmGUI As New frmGUI
        myfrmGUI.ShowDialog()
    End Sub
End Module
and then a routine to hide the form to the Systray when minimized
Code:
Public Class frmGUI
    Private Sub frmGUI_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            Me.Hide()
        End If
    End Sub
End Class
The problem is once the Minimize button is pressed, it transfers control back to "Sub Main()" which then ends Sub and thus quits.

Is there I can prevent this from happening without setting the Startup object to "frmGUI?"
 
Make sure the setting "Shutdown mode" is set to "When last form closes".

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Unfortunately, the only way to set the Shutdown Mode is to Enable Application Framwork. According to Visual Studio 2005, the "Startup object must be a form when 'Enable Application Framework' is checked."

I'd like to continue using the Sub Main() to start off the program if possible.
 
Try replacing:

[tt]myfrmGUI.ShowDialog()[/tt]

wth:

[tt]Application.Run(myfrmGUI)[/tt]


Not tested as I don't have VS on this machine.


Hope this helps.

[vampire][bat]
 
The problem lies in:

Code:
       If Me.WindowState = FormWindowState.Minimized Then
            Me.Hide()
        End If


This works:

Code:
Module Module1

	Public Sub Main()

		Dim f As New Form1
		Application.Run(f)

		'f.ShowDialog() does work, but I always use Application.Run

	End Sub

End Module

Code:
Public Class Form1

	Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

		If WindowState = FormWindowState.Minimized Then
			If Not NotifyIcon1.Visible Then NotifyIcon1.Visible = True
			Visible = False
		End If

	End Sub

	Private Sub NotifyIcon1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.Click

		Visible = True
		WindowState = FormWindowState.Maximized

		'if you don't want to hide the TrayIcon then don't use the next line,
		NotifyIcon1.Visible = False

	End Sub
End Class


Hope this helps.

[vampire][bat]
 
I didn't realize the option was dependent. Though I'm a little confused why sub main and create an instance. I could see if you wanted to run it like a service or something and you would create and instance when the user needed to change some options, but if you are going to open a form any way. You can add a sub new to a form to perform (from what I've tried) anything before the form actually opens. I'm just curious if there is another side benefit I don't know about.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks, everyone.. I need some pre-work done before a GUI is presented, but I need the flexibility of hiding/showing as necessary.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top