Sharing of Context Menus

E

eBob.com

My app is going to have an array of RichTextBoxes. I would like to have one
context menu for all of the RTBs. But I have learned that when a menu item
is clicked the "sender" for the click event is the menuitem. That makes
sense, of course, but I'll need to know which RTB the user was playing with.
So from the menuitem I'd like to trace back to the RTB, the "Tag" of which
will tell me which RTB the event handler needs to work with.

Even if I have to have a unique contextmenu for each RTB I am not sure how
that would work as I do not see a Tag property for either the MenuItem class
or the ContextMenu class.

Thanks for whatever help, advice, sympathy you can offer.

Bob
 
T

Tom Shelton

My app is going to have an array of RichTextBoxes. I would like to have one
context menu for all of the RTBs. But I have learned that when a menu item
is clicked the "sender" for the click event is the menuitem. That makes
sense, of course, but I'll need to know which RTB the user was playing with.
So from the menuitem I'd like to trace back to the RTB, the "Tag" of which
will tell me which RTB the event handler needs to work with.

Even if I have to have a unique contextmenu for each RTB I am not sure how
that would work as I do not see a Tag property for either the MenuItem class
or the ContextMenu class.

Thanks for whatever help, advice, sympathy you can offer.

Bob

MenuItem inherits from Menu - which has a Tag property.... At least as
of the 2.0 framework. Are you using VB2005 or latter?

One option, that might be cleaner is to wrap your RTB's and
ContextMenu's in a UserControl...
 
C

cfps.Christian

The context menu has a SourceControl property which tells what control
opened the context menu.

protected sub Item_Clicked(byval sender as object, byval e as
eventargs)
dim tmi as toolstripmenuitem = directcast(sender,
toolstripmenuitem)
dim cm as contextmenustrip = directcast(tmi.owner,
Contextmenustrip)
dim rtb as richtextbox = directcast(cm.SourceControl,
RichTextBox)
end sub
 
H

Herfried K. Wagner [MVP]

eBob.com said:
My app is going to have an array of RichTextBoxes. I would like to have
one context menu for all of the RTBs. But I have learned that when a menu
item is clicked the "sender" for the click event is the menuitem. That
makes sense, of course, but I'll need to know which RTB the user was
playing with.

Take a look at the value of the context menu object's 'SourceControl'
property. It contains a reference to the control the context menu has been
shown on.
 
T

Tom Shelton

Take a look at the value of the context menu object's 'SourceControl'
property. It contains a reference to the control the context menu has been
shown on.

Dang! I forgot about that one :( Boy, I must be getting old....
 
E

eBob.com

Thank you, and thanks too to Tom and Herfried for their prompt and helpful
responses.

I could not find an "owner" member for MenuItem. But I did find
"GetContextMenu". So the code I have now, and which seems to work, is ...

Private Sub LoadSampleTextFileMenuItemClick(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim whichone As WhichSampTextRTB
Dim thertb As RichTextBox
Dim ctxmenu As ContextMenu
Dim TheMenuItem As MenuItem
TheMenuItem = DirectCast(sender, MenuItem)
ctxmenu = TheMenuItem.GetContextMenu
thertb = DirectCast(ctxmenu.SourceControl, RichTextBox)
whichone = DirectCast(thertb.Tag, WhichSampTextRTB)
MsgBox("menu item click entered for " + whichone.i.ToString + ", " +
whichone.j.ToString)
End Sub

As I said, this SEEMS to work. But I'd appreciate knowing if it's not the
best way to get back to the RTB or if anyone has suggested improvements.

Thanks again for all of the responses, Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top