Solved

Dynamic Context Menu for Notify Icon

Posted on 2004-10-11
5
972 Views
Last Modified: 2012-06-22
How can a context menu for a notify icon be programmatically generated when the user right clicks on the notify icon?

Ie information/menu items gets added to the context menu just when the user right clicks the notify icon?
0
Comment
Question by:netslt
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
eozz_2000 earned 200 total points
ID: 12281954
Private Sub frmDynamic_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged
        Me.mnuIcon.MenuItems.Clear()
        If Me.Visible Then
            Dim mnuHide As New MenuItem("Hide")
            AddHandler mnuHide.Click, AddressOf Me.Hide_Click
            Me.mnuIcon.MenuItems.Add(mnuHide)
        Else
            Dim mnuShow As New MenuItem("Show")
            AddHandler mnuShow.Click, AddressOf Me.Show_Click
            Me.mnuIcon.MenuItems.Add(mnuShow)
        End If
    End Sub

Private Sub Hide_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.Hide()
    End Sub

Private Sub Show_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.Show()
    End Sub

I hope this could help you :)
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 total points
ID: 12304684
Add a ContextMenu to your form and set the ContextMenu property of your NotifyIcon to it.  Then, in the Popup event of your ContextMenu, you can dynamically create the MenuItems as shown below.  You can trap which menu item was selected using an old school select case based on the menuitem text:

    Private Sub ContextMenu1_Popup(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
        ContextMenu1.MenuItems.Clear()
        Dim myMenuItem As MenuItem

        myMenuItem = New MenuItem("Option1")
        ContextMenu1.MenuItems.Add(myMenuItem)
        AddHandler myMenuItem.Click, AddressOf mmi_Click

        myMenuItem = New MenuItem("Option2")
        ContextMenu1.MenuItems.Add(myMenuItem)
        AddHandler myMenuItem.Click, AddressOf mmi_Click
    End Sub

    Private Sub mmi_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim mmi As MenuItem
        mmi = CType(sender, MenuItem)
        Select Case mmi.Text
            Case "Option1"

            Case "Option2"

        End Select
    End Sub
0
 

Author Comment

by:netslt
ID: 12307198
Just one problem: As the menu item Texts are created on menu creation I don't know what these texts are (dynamic values, change every time menu gets created), so I am not able to hardcode a

Case "Option1"

etc.

So how do I handle the clicks on these variable entries (and still pass the value of this variable entry to the function that handles the clicks)?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12308212
What kind of dynamic values are you creating and what will you be doing with them?

As you can see from the code, you can determine which item was clicked by casting the "sender" variable to a MenuItem and querying the Text property.  You can use that value to pass to another function or do something with it.

~IM
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12313124
Jejeje... the code that Idle_Mind wrote is almost the same as mine...

Well... you must have a list of commands that you will be able to support with your application, I thik is imposible to create a comand that can execute all the things in the world.

I would prefer to create a catalog of functions and then, depending on the text in your new menu item, you can handle it with only one handler that checks the text of the sender.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question