Minimise to system tray by clicking minimise button

wickyd

Member
Joined
Oct 22, 2006
Messages
5
Programming Experience
Beginner
How do I minimise an application to the system tray when I click on the minimise button?

I can minimise when the user clicks on the Close button, because I can wrap the code inside ..._Closing:

VB.NET:
    Private Sub FormMain_Closing(ByVal sender As Object, _
        ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Me.ShowInTaskbar = False
        Me.Hide()
        e.Cancel = True 
    End Sub

I can minimise when the user presses the escape key, because I can wrap the code inside ..._KeyDown:

VB.NET:
    Private Sub FormMain_KeyDown(ByVal sender As System.Object, _ 
        ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Hide()
        End If
    End Sub

What is the event that I need to wrap this code around, so that it works when I click on the minimise button?

Thank you.

Kind regards
wickyd
 
The Form.Resize event where you check if Form.WindowState=Minimized
 
Hi JohnH

That is eactly what I was looking for.

For completeness, here is the code:

VB.NET:
PrivateSub FormMain_Resize(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) HandlesMyBase.Resize
    If Me.WindowState = 1 Then
         Me.Hide()
    End If
End Sub

Thank you.

Kind regards
wickyd
 
Last edited:
Hi jmcilhinney

Thank you for your suggestion.

My next concern was how to restore the window to a normal state after it had been minimised and hidden.

So to fix that, I used

VB.NET:
[COLOR=black][SIZE=2]Private[/SIZE][SIZE=2]Sub[/SIZE][SIZE=2] NotifyIcon_Click([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] sender [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.Object, _[/SIZE][/COLOR]
[COLOR=black] [SIZE=2]ByVal[/SIZE][SIZE=2] e [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2]Handles[/SIZE][/COLOR][SIZE=2][COLOR=black] NotifyIcon.Click[/COLOR][/SIZE]
[SIZE=2][COLOR=black] Me[/COLOR][/SIZE][SIZE=2][COLOR=black].Show()[/COLOR][/SIZE]
[COLOR=black][SIZE=2] If [/SIZE][SIZE=2]Me[/SIZE][SIZE=2].WindowState = FormWindowState.Minimized [/SIZE][SIZE=2]Then[/SIZE][/COLOR]
[SIZE=2][COLOR=black]      Me[/COLOR][/SIZE][SIZE=2][COLOR=black].WindowState = FormWindowState.Normal[/COLOR][/SIZE]
[COLOR=black][SIZE=2] End [/SIZE][SIZE=2]If[/SIZE][/COLOR]
[COLOR=black][COLOR=black][SIZE=2]End[/SIZE][SIZE=2] Sub[/SIZE][/COLOR]
[/COLOR]

Thanks for all the help.

Kind regards
wickyd
 
Back
Top