VB.Net Code Example for a 'Sub Main' Startup for a System Tray Application with an Infragistics Context Menu

· Randy Walker

It’s easy to create an application that uses the NotifyIcon control, you simply add the control to your windows form.  There are even code examples out there that show how to add a Notify Icon through code.  However I had a really hard time finding one that started up from ‘Sub Main’.  Add in the fact that I use Infragistics controls and I couldn’t find any examples of hooking up a PopupMenu Tool as the context menu … this took me several hours to hook it all together.

Enjoy …

Imports Infragistics.Win.UltraWinToolbars

Public Module Startup
    Private WithEvents SystemTrayIcon As New System.Windows.Forms.NotifyIcon
    Private WithEvents ToolbarManager As UltraToolbarsManager
    Private ContextMenu As PopupMenuTool

    Public Sub Main()
        SystemTrayIcon.Icon = New System.Drawing.Icon(”.\Images\Keys.ico
        SystemTrayIcon.Visible = True

        CreateContextMenu()
        Windows.Forms.Application.Run()
    End Sub

    Private Sub CreateContextMenu()
        Dim CurrentTool As ButtonTool

        ToolbarManager = New UltraToolbarsManager
        ContextMenu = New PopupMenuTool(“ContextMenu”)
        ToolbarManager.Tools.Add(ContextMenu)

        CurrentTool = New ButtonTool(“Help”)
        CurrentTool.SharedProps.Caption = “&Help”
        CurrentTool.SharedProps.AppearancesSmall.Appearance.Image = System.Drawing.Image.FromFile(”.\Images\help.png
        ToolbarManager.Tools.Add(CurrentTool)

        CurrentTool = New ButtonTool(“About”)
        CurrentTool.SharedProps.Caption = “&About”
        ToolbarManager.Tools.Add(CurrentTool)

        CurrentTool = New ButtonTool(“Exit”)
        CurrentTool.SharedProps.Caption = “E&xit”
        ToolbarManager.Tools.Add(CurrentTool)

        CurrentTool = ContextMenu.Tools.AddTool(“Help
        CurrentTool.InstanceProps.IsFirstInGroup = True
        CurrentTool = ContextMenu.Tools.AddTool(“About
        CurrentTool = ContextMenu.Tools.AddTool(“Exit
        CurrentTool.InstanceProps.IsFirstInGroup = True

        ToolbarManager.SetContextMenuUltra(SystemTrayIcon, “ContextMenu”)
    End Sub

    Private Sub ToolbarManager_ToolClick(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinToolbars.ToolClickEventArgs) Handles ToolbarManager.ToolClick
        Select Case e.Tool.Key
            Case “Exit”
                Windows.Forms.Application.Exit()
            Case “About”
            Case “Help”
        End Select
    End Sub
End Module