Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamic Context Menu for Notify Icon

Posted on 2004-10-11
5
Medium Priority
?
995 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 800 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 800 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

705 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