views:

423

answers:

1

How can I get the standard context menu in a RichTextBox, like the one in TextBox (Copy, Paste, IME etc.)? I'd go with a P/Invoke solution if nothing is available out of the box. Or do I really have to follow those strange advices to create a custom menu to get a standard menu (sigh)?

+1  A: 

The ContextMenuStrip property of the richtextbox can be assigned to a user designed context menu, Copy/Cut/Paste and just apply the RichTextBox's methods to each of the menu items such as CanUndo, CanCopy, CanPaste to determine the menu item's Enabled property. Using the methods 'Copy,CutandPaste` will be the executed method for the menu item's click event.

Hope this helps, Best regards, Tom.

tommieb75
It should be noted that the TextBox does indeed have their own standard context menu, by assigning null to it prevents the user from copying/pasting etc...
tommieb75
Unfortunately, that's just a custom context menu which I'd rather not have to create, especially with the more complicated options like IME and Insert Unicode Character etc. The mention of the Can* properties is a good advice, though, so +1 for that. I guess I'll do that when nobody can tell me how to get the standard menu. I'd really like to have the TextBox standard menu on a RichTextBox. Seems impossible, though, unfortunately.
OregonGhost
See here for an example of an extended RichtextBox.. http://www.codeproject.com/KB/miscctrl/richtextboxextended.aspxI wouldn't go far to implementing an IME and insert unicode character as that would be pinvoke involved...
tommieb75
Interesting thing. We're using the Krypton Toolkit in this particular project, though, so the RichTextBox is already encapsulated and I think we can't change the underlying class. On the other hand, the Krypton styling is just a thin border for a RichTextBox...
OregonGhost
I've accepted this answer for now. It seems to be the only way. The codeproject article is a convenient short-cut, but it's basically the same you already suggested.
OregonGhost
Today, I implemented this solution. It works, though CanCopy does not exist. I checked (SelectionLength > 0) instead for cut, copy and delete. I also had problems with the short-cuts not working properly because some other component in the main window blocked them. But it's fine now ;)
OregonGhost