adding a context menu left click event to a notify icon
Asked Answered
E

1

6

A notifyIcon is down in task bar, a user left clicks on it, how to show the contextmenu after left click?

Also, How to make it go away with another left lick anywhere else?

Thanks in Advance...!

Eardrum answered 19/2, 2011 at 14:40 Comment(1)
R
4

If your using Visual studio this is very easy, just add a notify icon to the form in the IDE then add a contextmenu item,

in order to see the notify icon it needs to have an icon(.ico) and the context menue needs to be populated with a menue structure .

in the notify icon properties set the ContectMenuStrip property to your context menue i.e Contextmenue1. By default if a user right clicks the icon it will show the menue and will hide it when they click elsewhere,

If you actualy need that to happen with left click then you need to code that

Private Sub NotifyIcon1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NotifyIcon1.Click

    ContextMenuStrip1.Show(Control.MousePosition)

End Sub

Geting it to close on left click is a bit different, if your form is still open you can use a click event in the main form

Private Sub Form1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick
    If ContextMenuStrip1.Visible = True Then
        ContextMenuStrip1.Visible = False

    End If
End Sub

If not then i would recomend that you tie in to mouse movement instaed of a click, i.e when the mouse leaves the menue it closes

 Private Sub ContextMenuStrip1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenuStrip1.MouseLeave
    ContextMenuStrip1.Close()


End Sub

Hope this is what you were looking for

Ridge answered 20/2, 2011 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.