how to access contextmenu for a system tray app started using applicationcontext

kawark

New member
Joined
Nov 5, 2009
Messages
4
Location
Little Rock, AR
Programming Experience
5-10
I'm having trouble figuring out how to access my system tray context menu from a winforms opened up from one of the menu choices. The task notifier was created using a module and an applicationcontext so I could start the application without a form.

Here is m simplified code.
VB.NET:
Module Module1
    Public OpenFormsHash As New Hashtable  

    Public Sub Main()
    	Application.Run(New MyContext)
    End Sub

End Module


Public Class MyContext
    Inherits ApplicationContext
    Private m_Icon1 As Icon

    Private WithEvents TrayIcon As New NotifyIcon

    'Context Menu/items for the Notification Area Icon
   Private WithEvents CMS As New ContextMenuStrip
    Private WithEvents mnuExit As New ToolStripMenuItem("Exit")
    Private WithEvents mnuManageSettings As New ToolStripMenuItem("Manage Settings")
    Private WithEvents mnuManagePermissions As New ToolStripMenuItem("Manage User Permissions")
    Private WithEvents mnuInfo As New ToolStripMenuItem("Help")
    Private WithEvents Seperator1 As New ToolStripSeparator()
    Private WithEvents Seperator2 As New ToolStripSeparator()
    Private Event OpenFormHandler(ByVal tab As String)

    Public Sub New()

        AddHandler OpenFormHandler, AddressOf ShowSettingsForm

        TrayIcon.Icon = m_Icon1
        TrayIcon.Text = "blah blah blah..."
        TrayIcon.Visible = True

        'Create the Context Menu
        CMS.Items.Add(mnuManageSettings)
        CMS.Items.Add(mnuManagePermissions)
        CMS.Items.Add(Seperator1)
        CMS.Items.Add(mnuInfo)
        CMS.Items.Add(Seperator2)
        CMS.Items.Add(mnuExit)
        CMS.Name = "cmsMain"
        TrayIcon.ContextMenuStrip = CMS

        My.User.InitializeWithWindowsUser()
        CurrentUser = My.User.Name.Replace("ADPCE\", "")


    End Sub

    Private Sub OnManageSettings(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuManageSettings.Click
        ShowSettingsForm("Settings")
    End Sub

    Private Sub OnManageUserPermissions(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuManagePermissions.Click
        ShowSettingsForm("Permissions")
    End Sub

    Private Sub MyContext_ThreadExit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ThreadExit
        TrayIcon.Visible = False
    End Sub

#End Region

    Private Sub ShowSettingsForm(ByVal tab As String)

        Dim fSettings As frmSettings
        If OpenFormsHash.Contains("frmSettings") Then
            fSettings = OpenFormsHash.Item("frmSettings")
            fSettings.BringToFront()
        Else
            fSettings = New frmSettings
            'fSettings.TopMost = True
            fSettings.InitialTabToDisplay = tab
            fSettings.Show()
        End If

    End Sub

End Class

When I start my form, in this case fSettings, I can't figure out how to get access back to my context menu. I'd like to modify it based on events that happen on the open form (fSettings).

Any ideas or help would be appreciated.
Thanks.
 
The simple way to do it is to pass the menu to the form, either as a constructor or method argument or by setting a property. It's up to you to declare the constructor, method or property in the form class.

The proper way to do it is for the form to raise an event and for the application context to handle that event and then edit its own menu. If you're interested in going that way, follow the Blog link in my signature and check out my post on Custom Events.
 
Thank you for your reply. BTW, your blog site is very nice and I recommend to anyone reading this to check out jmcilinney's blog.

I decided to use Events. I'm not sure why I couldn't see this before your reply but thanks for getting me going in the right direction again.
Here's basically what I did for anyone reading and want an answer:

In my Form Class I added an event that I will process in my application settings. I also wanted to pass back a few arguments so I decided to go ahead and also create an EventArgs class.
VB.NET:
    'Event to notify the Applicaiton Context that something has changed and
    'it needs to update the context menu
    Public Event SettingChanged(ByVal sender As Object, ByVal e As SettingChangedEventArgs)

Here is a simplified version of eventargs

VB.NET:
Public Class SettingChangedEventArgs
    Inherits System.EventArgs

    Private _SetingName As String
    Public Property SettingName() As String
        Get
            Return _SetingName
        End Get
        Set(ByVal value As String)
            _SetingName = value
        End Set
    End Property

    Private _SettingValue As String
    Public Property SettingValue() As String
        Get
            Return _SettingValue
        End Get
        Set(ByVal value As String)
            _SettingValue = value
        End Set
    End Property

End Class

So now that I have my event code set up in my Form Class I just need to go to my application context and add the code to handle the event once it is raised.

VB.NET:
Private Sub ShowSettingsForm(ByVal tab As String)

    Dim frm As New frmSettings
   
     'add a handler to process the event one the form raises it...
    AddHandler frm.SettingChanged, AddressOf OnSettingChanged

    frm.Show()

End Sub


Private Sub OnSettingChanged(ByVal sender As Object, e As SettingChangedEventArgs)
        'handles the event from the settings from when something changes

End Sub
 
Excellent work. I wish more people could take instruction and then put it to use the way you have.

The one thing that you may have missed is that, whenever you use AddHandler, you should use RemoveHandler as well. You should handle the form's FormClosed event as well and, in that event handler, remove the handlers for both SettingChanged and FormClosed.
 
Back
Top