RichTextBox: Bad Cut,Paste interactions with Menu Shortcuts Ctrl+X, Ctrl+Y 
Author Message
 RichTextBox: Bad Cut,Paste interactions with Menu Shortcuts Ctrl+X, Ctrl+Y

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.



Sat, 20 Oct 2001 03:00:00 GMT  
 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.



Sat, 20 Oct 2001 03:00:00 GMT  
 RichTextBox: Bad Cut,Paste interactions with Menu Shortcuts Ctrl+X, Ctrl+Y
Here's one way to fix it.

Create a formlevel boolean called bNoPaste.

In the RTB's KeyDown event, do something like this:

Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim bCtrlDown As Boolean
    bCtrlDown = (Shift And vbCtrlMask) > 0
    If KeyCode = vbKeyV And bCtrlDown Then
        bNoPaste = True
    End If
End Sub

In the KeyUp event, do this:

Private Sub RichTextBox1_KeyUp(KeyCode As Integer, Shift As Integer)
    bNoPaste = True
End Sub

Now in your menu code use:
    If Not bNoPaste Then
        .
        .
        .
    End If

That should fix it.

On Tue, 4 May 1999 16:05:15 -0400, "David Stevenson"

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.



Sat, 20 Oct 2001 03:00:00 GMT  
 RichTextBox: Bad Cut,Paste interactions with Menu Shortcuts Ctrl+X, Ctrl+Y
Re my previous post. The KeyUp event code should be:

Private Sub RichTextBox1_KeyUp(KeyCode As Integer, Shift As Integer)
    bNoPaste = False
End Sub



Sun, 21 Oct 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. RichTextBox <ctrl>V pastes 2 copies, <alt>EP pastes 1 copy

2. How TO : Automatic copy (ctrl-c) + clipboard + paste (ctrl-v) + save as filename

3. How TO : Automatic copy (ctrl-c) + clipboard + paste (ctrl-v) + save as filename

4. How TO : Automatic copy (ctrl-c) + clipboard + paste (ctrl-v) + save as filename

5. How TO : Automatic copy (ctrl-c) + clipboard + paste (ctrl-v) + save as filename

6. RichTextBox (Ctrl-V) Paste

7. RichTextBox paste(Ctrl+V) problem??

8. RichTextBox (Ctrl-V) Paste

9. RichTextBox (Ctrl-V) Paste

10. HOW CAN I DISABLE CTRL+X, CTRL+V,CTRL+C

11. HOW CAN I DISABLE Ctrl+C,Ctrl+V,Ctrl+X

12. disabling copy/paste/cut on shape shortcut menu

 

 
Powered by phpBB® Forum Software