Solved

Dynamic Context Menu for Notify Icon

Posted on 2004-10-11
5
957 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now