
RichTextBox: Bad Cut,Paste interactions with Menu Shortcuts Ctrl+X, Ctrl+Y
A co-worker found a solution in one of the newsgroups.
Sorry, the e-mail didn't include proper acknowledgement. Thank you Igor.
I've found an even simpler solution. It seems that the RichTextBox control
has the Ctrl+X, Ctrl+C and Ctrl+V events/methods built in to begin with.
You don't even need to implement them at all. Try starting a basic
application in VB and put just a single RTB control on it. Run it and try
to cut, copy, paste some text in it using the ctrl+? keys. You'll see that
it works without any code. Now if you let the built in stuff handle the
paste for the keystroke command, while implementing your own solution for
the menu and toolbar commands you'll soon realize that they act slightly
differently and that could be annoying.
So here's my solution. Let the built in code do the work for you. Instead
of implementing your own command handlers simply simulate the built in
keystrokes like this:
Private Sub mnuEditPaste_Click()
SendKeys "^v"
End Sub
Private Sub mnuEditCopy_Click()
SendKeys "^c"
End Sub
Private Sub mnuEditCut_Click()
SendKeys "^x"
End Sub
Oh, and don't forget to take the Ctrl+? shortcuts for these commands out of
the menu editor. Instead when the form loads do the trick that Rick has
proposed:
mnuEditPaste.Caption = "&Paste" & vbtab & "Ctrl+V"
for all 3 of the commands.
That's it. Hope this helps.
--Igor
Quote:
> I am trying to use Cut, Copy and Paste with Menu Shortcuts Ctrl+X, Ctrl+C,
> and Ctrl+V.
> It appears that RichTextBox (and possibly TextBox) automatically processes
> Ctrl+X
> and Ctrl+V, so that each Cut and Paste operations are done twice: once by
> the
> automatic code associated with RichTextBox, and once manually with the Menu
> button event handler.
> To set up the test cases:
> Added RichTextBox to Standard Form.
> Added menu:
> Caption Name Shortcut
> -------------------------------------------------------
> Edit mnuEdit
> Cut mnuEditCut Ctrl+X
> Paste mnuEditPaste Ctrl+V
> Private Sub mnuEditCut_Click()
> Debug.Print "**" & RichTextBox1.SelText & "**"
> On Error Resume Next
> Clipboard.SetText RichTextBox1.SelText
> RichTextBox1.SelText = vbNullString
> End Sub
> Private Sub mnuEditPaste_Click()
> On Error Resume Next
> RichTextBox1.SelText = Clipboard.GetText
> End Sub
> Run application. Select "Box1". Ctrl+X. Paste into Notepad. No text is
> pasted.
> Comment out Cut code. Re-run Application as above. It works now. "Box1" is
> pasted successfully.
> With Cut code commented out, re-run application.
> Select "Box1". Ctrl+X. Ctrl+V. "Box1Box1" is pasted.
> You're probably saying, either delete the code, and everything will work
> fine, or delete
> the Menu shortcuts and everything will work fine.
> Reasons against deleting the shortcuts
> Menu Shortcuts document the keystrokes necessary to use the shortcut.
> I will want to use a Ctrl+X (cut) to cut other controls: i.e.
> TreeViews/ListViews with File Lists.
> i.e. I want to cut and paste lists of files. (I may have to simulate this
> somehow with collections).
> Reasons against deleting the code.
> We don't know how we got to mnuEditCut_Click. By selecting Cut from the Edit
> menu (here
> we need the code), or by Ctrl+X, which automatically runs the RichTextBox's
> cut behavior
> (Ok to delete code in this latter case, but the the former case).
> Any help or suggestions would be appreciated.