Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1013
  • Last Modified:

Dynamic Context Menu for Notify Icon

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
netslt
Asked:
netslt
  • 2
  • 2
2 Solutions
 
eozz_2000Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
netsltAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
eozz_2000Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now