ContextMenu Popup event fires twice in RichTextBox.

J

jdlwright

Hi,

I can barely believe that searching for "contextmenu richtextbox event"
in google groups found only a handful of results and none relevant to
this problem... so it must be me doing something wrong (although, I can
double-barely-believe that ;-))


The problem is, if I press the Apps key while in a RichTextBox, to
trigger my context menu, the Popup event is fired twice (whereas if I
use the right mouse button, it's fire once). This problem doesn't
exist with TextBox.

Steps to reproduce.

1. Drop a RichTextBox and ContextMenu onto a Form

2. Set RichTextBox.ContextMenu to your ContextMenu

(3. I added a menu item, but maybe this is unimportant)

4. Add an event listener to the Popup event

this.contextMenu1.Popup += new
System.EventHandler(this.contextMenu1_Popup);

5. put a breakpoint on your event handler method

6. press the Apps key to trigger the CM inside the RTB, and you'll see
the break point hit twice (btw I checked to make sure that the
duplicate event wasn't caused by using breakpoints to debug - I did
this using an int counter and a break point on a button).


So is this a bug/"it's-by-design ;-/"? Any elegant solutions??
Thanks,

Jim
 
C

Claes Bergefall

I can reproduce it.

The RTB implements IRichEditOleCallback interface and it is its
GetContextMenu method that fires the popup event (and then
shows the menu) when you right click and the first time when
you use the App key.

The second occurance of the popup event when pressing the App
key is due to the normal WM_CONTEXTMENU processing.

To me, this looks like a bug. There is a way around it though
Inherit the RTB and override it's WndProc. Handle the
WM_CONTEXTMENU message and simply return without
calling the base class: Here is the VB code for it:

Private Const WM_CONTEXTMENU As Integer = &H7B
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_CONTEXTMENU Then
m.Result = IntPtr.Zero
Else
MyBase.WndProc(m)
End If
End Sub

I would expect a WM_CONTEXTMENU when right clicking aswell
but that is not the case!? It looks like Windows suppresses the generation
of a WM_CONTEXTMENU message when you right click but not
when pressing the App key (all assuming EM_SETOLECALLBACK
has been used). Thus the real problem seems to be with the RTB control
in Win32 and not with the .NET wrapper of it.

I only found one post in google groups that mentions the problem:
<http://groups.google.se/groups?q=WM_CONTEXTMENU+VK_APPS&hl=sv&lr=&selm=sh7c
ouokollb47c04amin3qniujtsdvnlv%404ax.com&rnum=6>
It's about MFC but the basic problem is the same (altough the
results and solution differ)

/claes
 
J

jdlwright

Thanks Claes, that's a great solution and I will implement it.

Anybody at MS care to talk about the future of RichTextBox? Will this
bug be fixed? Will the RTB contol ever be replaced? - with a pure .NET
control?

Personally I'd like to see a pure .NET version, with an API as open as
Java's JTextPane (you can do practically anything with that).
Thanks again Claes.

Jim
 

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