Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can merger my own ContextMenu with the default one?

Status
Not open for further replies.

dt114

Programmer
Dec 10, 2003
4
US
I want to merger some menuitems, such as "Undo", "Redo" into the standard TextBox ContextMenu. The standard TextBox ContextMenu already has "Copy","Paste", etc. I want to keep those menuItems, but add some more. I can create my own ContextMenu with those items "Copy","Paste",.... But if I do so, I have to rewrite all the codes for the "Copy","Paste",etc. How can I keep those default functions in the standard TextBox ContextMenu, and just merger my own contextMenu with items such as "Undo","redo" to it?
 
I haven't tested this code, but it looks like it'll work.

Basic idea:
You use the ContextMenu property of the TextBox object to get a reference to a ContextMenu object. You can then call the .Add method on it's MenuItems property to add a new MenuItem.

Code:
' In your load event:
TextBox1.ContextMenu.MenuItems.Add("&Undo", New EventHandler(AddressOf 
HandleUndo))

' Elsewhere in your form's code:
Protected Sub HandleUndo(sender As System.Object, e As System.EventArgs)
    ' Insert code to handle Undo request
End Sub

Hope this helps.
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
The "default" context menu for a textbox is handled by windows, not visual basic. When you add your own context menu it overrides the windows version. I have not found a way to 'add' to the windows context menu yet though, so good luck on that :)



Becca

Somtimes, the easy answer is the hardest to find. :)
 
Hi dt11
You can disable the System default menu of textbox and bind your Contextmenu to the textbox here is the code for that
Step 1) Declare in declaration section of your form
Private Declare Function LockWindowUpdate _
Lib "User32" (ByVal hwndLock As Long) As Long

'mnuPopUp is a context menu which you can make at design time
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
If e.Button.Equals(MouseButtons.Right) Then
'avoid disabled control's gray text
LockWindowUpdate(TextBox1.Handle.ToInt64)
TextBox1.Enabled = False
'allow txtInput to become disabled

TextBox1.ContextMenu = mnuPopUp
TextBox1.Enabled = True
LockWindowUpdate(0&)
End If
End Sub

Nouman Zaheer
Software Engineer
MSR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top