Link to home
Start Free TrialLog in
Avatar of hddp666
hddp666

asked on

Ctrl-V pastes twice in RichTextBox

I have a RichTextBox on a form. I have edited the menu for Edit>>Paste to make Ctrl-V a shortcut. The problem is that when I use Ctrl-V, it pastes twice in succession. If I use Alt-E-P, or click, it pastes only once. If I disable the declarations for the Edit>>Paste_Click() sub, Ctrl-V will still paste, but Alt-E-P or click do not. I can eliminate Ctrl-V as a shortcut in the menu editor, and Ctrl-V still works; however, the user will not see Ctrl-V in the menu, or know it's a valid way to paste.

How do I stop the RichTextBox from pasting twice if I set Ctrl-V as a shortcut? I have noticed that WordPad, which also uses the RichTextBox, has Ctrl-V listed in the menu as a shortcut, but there is no problem with double-pasting. I have VB5.0, fixed with SP3, on Win95.
Avatar of SLE
SLE

CTRL-V has standard paste functionality in most controls. So I suggest you simply put the following code in the Paste_Click event:

SendKeys "^v"

Or am I missing the point here?

Absolutely! SLE is right. Ctrl+V is already assigned to "Paste".

It is standard windows functionality, exactly like Shift+Ins, and right-clicking on a control and choosing "Paste" from the popup menu. Because it is standard windows functionality, you should NOT provide a menu yourself, simply assume the user knows how to do this.
Avatar of hddp666

ASKER

I know that ctrl-V is the standard paste function for most controls, and that it works fine for RichTextBox controls. However, I would like the menu to have this listed, as I cannot assume that the user will know this a priori. The present application doesn't matter, as it is an encryption utility for saving all of my usernames and passwords (I want to only have to ABSOLUTELY remember the encryption key).

I suppose I can trap the ctrl-V keypress event, and then call the paste sub. I will try this, and award if it works. However, I would really like to know how to fix this problem, so that any future programs will not need this kludge.
"as I cannot assume that the user will know this a priori"

I think you can - it's standard windows functionality so you can assume the user learnt this when they learnt how to use Windows.
Avatar of hddp666

ASKER

caraf_g,

You obviously have never written software for general use before. I have had idiots call me up, looking for the "Any Key" because their computer is telling them to press any key to continue. One time, this moron was unable to delete files from the Explorer window because he couldn't keep his hand still while right-clicking and windows kept telling him that he couldn't move the icon. To assume that people will know what CTRL-V does "when they learn Windows...." is completely ludicrous, as most people NEVER "learn Windows."

Avatar of hddp666

ASKER

SendKeys "^v" does not trap the ctrl-V keypress, it sends it. If I use this code in the menu_click declaration, I still wind up with double pasting when I use ctrl-V. I already know I can remove ctrl-V as a shortcut from the menu, and get rid of the double-pasting. However, I want to be able to show ctrl-V on the menu as a shortcut, and not assume that the user is experienced.

This double-pasting problem does not exist for the regular TextBox control. I can show ctrl-V in the menu, and it will not double paste. This problem is specific to the RichTextBox, and maybe only for VB5.
ASKER CERTIFIED SOLUTION
Avatar of header
header

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hddp666

"You obviously have never written software for general use before"

LOL - I've been developing software for over ten years now in commercial applications. But I see from your profile that you have quite a lot of experience yourself. So let's not let this deteriorate into some silly slagging match.

I appreciate your sentiment about trying to make your software moron proof but I have to warn you. Even in the days of the great German philosophers they realised something like that just could not be achieved. I think it was Friedrich Nietzsche (?) who said "In the face of stupidity even the gods are powerless".

But never mind, you obviously have decided that you must have this menu option. And you're in good company - my Internet browser has it, Notepad.exe has it, and VB itself has it too, so in actual fact it must be some sort of standard.

Back to the question.

If you have to do something different for a rich text box, why not use the following method to determine whether you're on one:

If Me.ActiveControl Is Nothing Then
    'something's amiss - no current control selected
    Exit Sub
End If
If TypeOf Me.ActiveControl Is RichTextBox Then
    'do something different
End If

Avatar of hddp666

ASKER

Header, your answer is this: "You could set a boolean variable in the keypress event of the richtext box when ctrl-v is pressed.  Then in the Menu ctrl-v click event you could say, if the variable is true then the user pressed ctrl-v and you could exit sub."

I know I can do this sort of thing; I was going to do something like see if CTRL-V was pressed in Menu_Click, and if so, disable the paste declaration.

However, the answer I am looking for is a way to stop the RichTextBox from acting this way. The regular textbox does not do this double-pasting thing, even if it is in the menu_click declarations.

Do you know how to actually solve this RichTextBox problem, or is it endemic to VB5.0?
I have never heard of this happening, it just seems like checking a variable would work.  Your question was, "How do I stop the RichTextBox from pasting twice if I set Ctrl-V as a shortcut?"  If my answer works, great!  Otherwise, reject it.

If you are dead set on finding another way, more power to you, I just don't see the point of going to the trouble if you already have another way of doing it.
Avatar of hddp666

ASKER

Header, I will try your answer and give you the points. I thought that maybe someone else had this problem, and there was a way to fix it, like an updated msvbvm50.dll, or some property I had overlooked. I know the VB5.0 RichTextBox control had other problems, like weird scrollbars, that were fixed by the VS_SP3 update.