3 Replies - 5417 Views - Last Post: 26 October 2015 - 06:36 AM

#1 Cal-cium   User is offline

  • New D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 26
  • Joined: 27-January 15

Minimize program to system tray (Notification Area)

Posted 07 August 2015 - 05:11 AM

Hi this is my first snippet, kind of a simple and straight forward to achieve, but I didn't see one on here of how to do it yet (Maybe). Basically the code I will display here will minimize your program to the system tray (Notification Area) based on whether the user wants the program minimized or not.

First of all you will need to create a CheckBox and rename it to an appropriate name. I've renamed mine to cbMinimize. It doesn't necessarily need to be a CheckBox but for this example I have done. I have previously used this code as a button in a MenuStrip.

Second thing to do will be to insert a NotifyIcon into your program. This is where the code that will minimize your program will go.

#Region "Notification Minimalize code"
    'When you double click the notify icon in the system tray(notification area) the form automatically shows
    'and the notify icon disapears.
    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        If cbMinimize.Checked = True Then
            Me.Show()
            Me.StartPosition = FormStartPosition.CenterScreen
            Me.WindowState = FormWindowState.Normal
            NotifyIcon1.Visible = False
        End If
    End Sub

    'This event handles the part where you minimize the main form, hides it and shows the notify icon.
    Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If cbMinimize.Checked = True Then
            If Me.WindowState = FormWindowState.Minimized Then
                Me.Hide()
                NotifyIcon1.Visible = True
            End If
        End If
    End Sub
#End Region




The code above will only make the program minimize to the system tray if cbMinimize has been checked by the user.

Also you will need to select an icon for the NotifyIcon or once minimized it won't appear.

Is This A Good Question/Topic? 2
  • +

Replies To: Minimize program to system tray (Notification Area)

#2 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,535
  • Joined: 07-September 13

Re: Minimize program to system tray (Notification Area)

Posted 22 October 2015 - 11:48 PM

Nice snippet :D

May be worth while also putting on about the fact that when showing secondary forms if you don't pass in a owner then they will not be hidden and you would have to recursively find each window open and hide them.

Or you can just pass a owner into that form and it all works as it should:

Passing through a owner:
Dim second as SecondForm
second.show(me)


Not passing through a owner:
Dim second as SecondForm
second.show()


If you have a application that is 3+ forms deep for example form1 opens form2 which opens form3 then you don't need to pass form1 into form3 you just need to pass form2 so it still would be show(me), this in effect links the forms so when something happens to the owner (like a hide) then they will all be hidden.
Was This Post Helpful? 1
  • +
  • -

#3 Cal-cium   User is offline

  • New D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 26
  • Joined: 27-January 15

Re: Minimize program to system tray (Notification Area)

Posted 23 October 2015 - 08:48 AM

That's a good thought, I haven't actually tried doing this for a program that has more than one form.

You've given me something to work on :) When I get time, I will try your method out. Thanks
Was This Post Helpful? 0
  • +
  • -

#4 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,535
  • Joined: 07-September 13

Re: Minimize program to system tray (Notification Area)

Posted 26 October 2015 - 06:36 AM

Your welcome, we are all here to learn off each other.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1