Solved

Dynamic Context Menu for Notify Icon

Posted on 2004-10-11
5
988 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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 86

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

Technology Partners: 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!

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

630 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