ContextMenu

W

wandii

Hi,
I am trying to show the shortcut menu on a richtextbox control by
adding the contextmenu component onto the form. The richtextbox
property is assigned to the contextmenu. I created my shorcuts, copy,
paste, etc. However, on the form I have a datagrid
which is also assigned to the same contextmenu and contextmenupopup
event as follows:
....
If ContextMenu1.SourceControl Is richtextbox1Then
' Add MenuItems to display for the TextBox.
ContextMenu1.MenuItems.Add(menuItem1)
ContextMenu1.MenuItems.Add(menuItem2)
ElseIf ContextMenu1.SourceControl Is datagrid1 Then
' Add the MenuItem
ContextMenu1.MenuItems.Add(menuItem3)
End If
....
when I right-click on the richtext control I get the sourcecontrol =
Nothing, but r-click on grid
i get the control name. I droped the richtext control and added another
one and set the
cmenu property and still nothing. Any idea?

Thanks in advance
 
W

wandii

Dennis said:
Need some more code, like the event handler sub declaration and what it
handles.

Thanks for your time Dennis. Here you go... I have the property of
datagrid and richtext
are assigned to contextmenu. Works fine for the datagrid and I get
nothing for the rtext. Thanks

Public Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles ContextMenu1.Popup

' Define the MenuItem objects to display for the TextBox.
Dim menuItem1 As New MenuItem("&Copy")
Dim menuItem2 As New MenuItem("&Find and Replace")
' Define the MenuItem object to display for the PictureBox.
Dim menuItem3 As New MenuItem("C&hange Picture")

' Clear all previously added MenuItems.
ContextMenu1.MenuItems.Clear()

If ContextMenu1.SourceControl Is richtextbox1Then
' Add MenuItems to display for the TextBox.
ContextMenu1.MenuItems.Add(menuItem1)
ContextMenu1.MenuItems.Add(menuItem2)
ElseIf ContextMenu1.SourceControl Is datagrid1 Then
' Add the MenuItem to display for the PictureBox.
ContextMenu1.MenuItems.Add(menuItem3)
End If
 
G

Guest

Your code looks fine. Are you able to type information into the richtextbox
to be sure it can get the focus? Also, does the contextmenu1 appear when you
right click on the richtextbox?
 
W

wandii

Sorry Dennis I could not get back to you soon. Yes I tried to setfocus
to richtext on form load and can type into richtext, however, no
contextmenu appears when I r-click on
it. I put a breakpoint please see below:
' Add MenuItems to display for the TextBox.
ContextMenu1.MenuItems.Add(menuItem1)
ContextMenu1.MenuItems.Add(menuItem2)
ElseIf ContextMenu1.SourceControl Is datagrid1 Then
' Add the MenuItem
ContextMenu1.MenuItems.Add(menuItem3)
End If
and sourcecontrol is nothing. Plan B: deleted the old richtext control
and added a new one still sourcecontrol is nothing. I tried on a
button, datagrid and sourcecontrol is not
null. Richtext's contextmenu property is to the contextmenu2. This is
very strange?
I am not sure if I need to set another property. Any other
suggestions. Thanks
 
G

Guest

Try this code (note the "sender" in the if statments:


Public Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles ContextMenu1.Popup

' Define the MenuItem objects to display for the TextBox.
Dim menuItem1 As New MenuItem("&Copy")
Dim menuItem2 As New MenuItem("&Find and Replace")
' Define the MenuItem object to display for the PictureBox.
Dim menuItem3 As New MenuItem("C&hange Picture")

' Clear all previously added MenuItems.
ContextMenu1.MenuItems.Clear()

If Sender Is richtextbox1Then
' Add MenuItems to display for the TextBox.
ContextMenu1.MenuItems.Add(menuItem1)
ContextMenu1.MenuItems.Add(menuItem2)
ElseIf Sender Is datagrid1 Then
' Add the MenuItem to display for the PictureBox.
ContextMenu1.MenuItems.Add(menuItem3)
End If
 
W

wandii

Nope - didn't work. The reason is that the sender listed below is
looking at the contextmenu (1, 2, ...) controls.
 
G

Guest

Sorry, I don't know what to try next. Are you sure you spelled the name of
the richtext box correctly? You might want to add a second contextmenu
dedicated to your richtextbox.
 

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

Similar Threads


Top