Sunday, January 27, 2008

CtrlV Shortcut causes a conflict with a Paste Menu Item

I have a form with a Menu Item, that I call "mnuPaste" that has a CtrlV shortcut associated to it.   On this form I have a TextEntry control.  If you try to paste text into the TextEntry control by hitting Ctrl-V, it will not paste because the Ctrl-V gets consumed by the mnuPaste Menu Item.

Now I still haven't found the perfect solution.  Ideally I would like the innermost control to have precedence.  So first TextEntry gets the Ctrl-V, then if we are not on the TextEntry control,  the Form's mnuPaste has precedence.  However, I can't seem to get this going.

The best I can do is direct the Ctrl events to the ActiveControl like this:

In the Form declare this function:

Private Declare Auto Function SendMessage Lib "user32" ( ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr ) As IntPtr

Then overide ProcessCmdKey in the Form:

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
  SendMessage(
Me.ActiveControl.Handle, msg.Msg, msg.WParam, msg.LParam)
End
Function

You can also use this type of call to send Ctrl from the MDI Parent to the Child form.  In that case you would replace Activecontrol with ActiveMdiChild.






No comments: