Groups | Blog | Home
all groups > vb.net > april 2005 >

vb.net : How to catch a right-click


Terry Olsen
4/9/2005 4:21:00 PM
How can I catch a right-click on a DropDownMenuItem?

Dennis
4/9/2005 6:29:01 PM
I too would like to know. It's too bad that M'soft just passes the EventArgs
type object to the click event...it would seem that they could have passed
the MouseEventArgs type instead...the EventArgs is useless

[quoted text, click to view]
Adam Goossens
4/10/2005 12:00:00 AM
Hi Terry,

To my knowledge there isn't a way to determine whether a click on a menu
item was a right or a left click (well, probably no simple way).

Why do you need to catch right clicks? Perhaps there's an alternate way
to do what you're doing without using MenuItems.

Regards,
-Adam.

[quoted text, click to view]
Terry Olsen
4/10/2005 8:03:31 AM
I want to be able to right-click a DropDownMenuItem and have a context menu
popup that allows me to delete that MenuItem. Like you can do in IE.

[quoted text, click to view]

Howard Kaikow
4/10/2005 10:33:38 AM
[quoted text, click to view]

There must be some way as Word has a DocumentBeforeRightClickEvent.
Dunno how it works internally.

Terry Olsen
4/10/2005 12:12:40 PM
I've found a way using the MouseDown event for the MenuItem. However, I
have dynamically created menu items where I create MenuItem object, add it
to the MenuBar, and then let go of the object. So without the object, I
can't catch the right-click event. I guess I've got to come up with a way
to hang on to each dynamically created MenuItem object so I can catch the
right-click and remove the item from the MenuBar.

[quoted text, click to view]

Dennis
4/10/2005 2:41:03 PM
I couldn't find where the MenuItem had a MouseDown event...are you sure.
Also, if you are "letting go of the object" by setting it to nothing or
disposing it, you can no longer show it in the Menu bar or Context Menu
PopUp, or whatever. What do you mean by "letting go"? If you can display
it, it's still there and you should be able to get to it's events.

[quoted text, click to view]
Terry Olsen
4/10/2005 6:59:24 PM
The following routine is what I use to add the MenuItem to a menu. As you
can see, the scope of the object is local to the Subroutine, so once I leave
the routine, I no longer have access to it, but it has been added to the
menubar so it continues to display and I can click on it and respond to it
using the 2nd routine listed below. The only way I can figure out how to
catch a right-click is to make a global array of MenuItems and add event
handlers for each one. Then I'll be able to respond to MouseDown events.

Sub AddFavoriteToMenu(ByVal FavoriteName As String)

Dim MenuItem As New ToolStripMenuItem

MenuItem.Text = FavoriteName

FavoritesToolStripMenuItem.DropDownItems.Add(MenuItem)

End Sub

Private Sub FavoritesToolStripMenuItem_DropDownItemClicked(ByVal sender As
Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs)
Handles FavoritesToolStripMenuItem.DropDownItemClicked

Dim tmp As String = e.ClickedItem.ToString

If tmp = "A&dd" Then Exit Sub

FavoriteMenuItemClicked(e.ClickedItem.ToString)

End Sub

[quoted text, click to view]

Dennis
4/11/2005 5:10:01 PM
That's the Click event, not the mousedown event. Also, the MenuItem is still
accessable as a menuitem in the FavoritesToolStripMenuItem.DropDownItems
collections, i.e. FavoritesToolStripMenuItem.DropDownItems(0) or 1 or
whatever. I also didn't find the DropDownItems Property/Method for any
control...what type is used to instantiate the instance of
FavoritesToolStripMenuItem? Are you using a custom third party control?

[quoted text, click to view]
Terry Olsen
4/11/2005 7:17:59 PM
Exactly. The click event is the only thing I have access to using the
FavoritesToolStripMenuItem.DropDownItems_Click handler. I can only see
which item under FavoritesToolStripMenuItem that I've clicked on, not
whether it was a right or left click (in fact, the event doesn't even
trigger to a right-click). If I look at a MenuItem that was added at design
time, then I have access to the MouseDown event where I can catch the
right-click event. My problem is that I need to catch the MouseDown event
for the dynamically added MenuItems, then I can pull the right-click out of
that.

[quoted text, click to view]

Dennis
4/12/2005 4:55:07 PM
I don't see the mousedown event in the MenuItem Class...are you sure it has a
MouseDown event..all I saw was the Click Event for this class.

[quoted text, click to view]
Terry Olsen
4/12/2005 9:55:18 PM
Let me double-check what I've got...we may be talking about different things
here...


[quoted text, click to view]

AddThis Social Bookmark Button