Hack Forums
[.NET] [VB.NET]How to Attach Context Menu with System Tray Icon - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [VB.NET]How to Attach Context Menu with System Tray Icon (/showthread.php?tid=407230)



[VB.NET]How to Attach Context Menu with System Tray Icon - codepoet - May 7, 2010


Author: DevASP
Tutorial: Crow407

In this simple tutorial you will learn that how can you attach context menu with system tray icon and also do tasks on click of this icon.

Create a New Project in VB.Net. Drag a NotifyIcon, Timer and Context menu from tool box and place on form. Now create options on the context menu like simple menu. Now start writing code on different events .

Declare Global variables:
Code
Dim icon1 As New Icon("ac0036-48.ico")
Dim icon2 As New Icon("ac0036-32.ico")
Dim check As Integer = 0
Dim NAMECHECK As Integer = 0


Write code on from load event:
Code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Timer1.Interval = 300
        Me.Icon = icon1
        NotifyIcon1.Icon = Me.Icon
        Me.NotifyIcon1.Visible = True
        Me.NotifyIcon1.ContextMenu = Me.ContextMenu1

End Sub


Write code on timer tick event:
Code
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If check = 1 Then
            Me.Icon = icon2
            NotifyIcon1.Icon = Me.Icon
            check = 0

        Else
            Me.Icon = icon1
            NotifyIcon1.Icon = Me.Icon
            check = 1
        End If
        If NAMECHECK = 0 Then
            Me.Text = "D"
        ElseIf NAMECHECK = 1 Then
            Me.Text = "DE"
        ElseIf NAMECHECK = 2 Then
            Me.Text = "DEV"
        ElseIf NAMECHECK = 3 Then
            Me.Text = "DEVA"
        ElseIf NAMECHECK = 4 Then
            Me.Text = "DEVAS"
        ElseIf NAMECHECK = 5 Then
            Me.Text = "DEVASP"
        End If
        NAMECHECK += 1
        If NAMECHECK = 6 Then
            NAMECHECK = 0
        End If


End Sub


Now write code on context menu items:
Code
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click

        NotifyIcon1.Visible = True
        Timer1.Start()

End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click

        Timer1.Stop()

End Sub

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click

        Me.Close()

End Sub


If You Like this tutorial, please check out my other tutorials by clicking ==> H3R3

[img]http://i685.photobucket.com/albums/vv215/blackpearl4/THANKS.gif[/img]



* - Scrat - May 7, 2010

Thankssss this is going to be very useful!!! I was searching for this for long time...


* - TehShrink - May 7, 2010

Knew this already. But good tutorial, good job!


* - codepoet - May 7, 2010

(May 7, 2010 02:22 PM)Ma3x*Ne0 Wrote:  Thankssss this is going to be very useful!!! I was searching for this for long time...

glad i could help....
(May 7, 2010 02:26 PM)TehShrink Wrote:  Knew this already. But good tutorial, good job!

thank you


* - Troxin - May 7, 2010

I will need this sometime :D

I was just fucking around on a blank project trying to get this to work, but couldn't do it :P


* - codepoet - May 7, 2010

well you have to rewrite some parts of it to meet your needs...