Resolved Unable to Paste into Textbox using Ctrl+V

Chris M

Member
Joined
Feb 15, 2021
Messages
15
Programming Experience
Beginner
Hi

I'm having trouble pasting text into a textbox by trapping the Control + V Keys in the textbox KeyDown event. KeyPreview for the form is set to True.

I've tried the following methods in the textbox keydown but neither works:

VB.NET:
If e.KeyCode = Keys.V AndAlso e.Modifiers = Keys.Control Then
    If Clipboard.ContainsText Then Me.Text_Convert.Text &= Clipboard.GetText
End If

VB.NET:
If e.KeyCode = Keys.V AndAlso (e.Control) Then
    If Clipboard.ContainsText Then Me.Text_Convert.Text &= Clipboard.GetText
End If

The code(s) seem to trap one key but not both?

I'm sure there's a simple solution but I'm not seeing it - hope someone can help.

Thanks for looking,

Chris.
 
Solution
If you want a Paste menu item, why not actually use it? When shortcut is pressed it's Click event is raised, where you can perform your custom paste. You may use ActiveControl to conditionally do that and regular TextBox.Paste if there are other textboxes.

Chris M

Member
Joined
Feb 15, 2021
Messages
15
Programming Experience
Beginner
Update: The following code works with pretty much any key other than X, C, V (the cut, copy & paste keys I'm trying to use!)

VB.NET:
If e.KeyCode = Keys.A AndAlso (e.Control) Then
    If Clipboard.ContainsText Then
        If Me.Text_Convert.Text = "0" Then
             Me.Text_Convert.Text = Clipboard.GetText
        Else
            Me.Text_Convert.Text &= Clipboard.GetText
        End If
          Me.Text_Convert.SelectionStart = Me.Text_Convert.Text.Length
       End If
End If

Any ideas? I really need to be able to just hit Ctrl+V on the keyboard to paste into the textbox. Frustrating.

Chris.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,457
Location
Sydney, Australia
Programming Experience
10+
Firstly, if you want to trap Ctrl+A specifically then this is not the right code:
VB.NET:
If e.KeyCode = Keys.A AndAlso (e.Control) Then
That will trap the A key with any combination of modifiers that includes the Ctrl key. You would actually need this:
VB.NET:
If e.KeyCode = Keys.A AndAlso (e.Control) AndAlso Not e.Shift AndAlso Not e.Alt Then
A better option is to use e.KeyData, which is the current key and the current modifiers:
VB.NET:
If e.KeyData = (Keys.Control Or Keys.A) Then
The Or can be confusing because, intuitively, it feels like it should be an And but this is a bitwise Or. If you understand how bitwise logic works, the fact that it's an Or makes perfect sense. If you don't understand how bitwise logic works, learn.

EDIT: I just reread your first post and saw this:
VB.NET:
If e.KeyCode = Keys.V AndAlso e.Modifiers = Keys.Control Then
That would indeed trap just the Ctrl+V key combination.
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,457
Location
Sydney, Australia
Programming Experience
10+
Have you set ShortcutsEnabled to False on the TextBox? I just created a new project, added a TextBox and a Button and then added this code:
VB.NET:
Public Class Form1

    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyData = (Keys.Control Or Keys.V) Then
            TextBox1.AppendText(Clipboard.GetText())
        End If
    End Sub

End Class
When I ran the project, if I pressed Ctrl+V when the Button had focus then it worked as expected but, if I pressed those keys when the TextBox had focus, the text was pasted twice. That's once by this code and then once by the TextBox. If I set ShortcutsEnabled to False on the TextBox, the code worked the same way when the Button had focus but nothing was pasted when the TextBox had focus. One solution I found was to leave ShortcutsEnabledset to True and then use this:
VB.NET:
Public Class Form1

    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyData = (Keys.Control Or Keys.V) Then
            TextBox1.AppendText(Clipboard.GetText())
        End If
    End Sub

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyData = (Keys.Control Or Keys.V) Then
            e.SuppressKeyPress = True
        End If
    End Sub

End Class
 

Chris M

Member
Joined
Feb 15, 2021
Messages
15
Programming Experience
Beginner
First of all, thanks for your reply.

I've tried your code(s) but none of them will work for me, regardless of whether or not the Form KeyPreview property is set to True or False, or whether or not the TextBox ShortcutsEnabled property is set to True or False (in any of the four potential combinations).

I have also tried tried just naturally letting Windows Paste the text into the TextBox, with no added code in the KeyDown event, but still no joy.

The code example I gave earlier is the only combination I've got to work (with KeyPreview = True, ShortcutsEnabled = True):

VB.NET:
If e.KeyCode = Keys.A AndAlso (e.Control) Then
     If Clipboard.ContainsText Then
        If Me.Text_Convert.Text = "0" Then
              Me.Text_Convert.Text = Clipboard.GetText
           Else
              Me.Text_Convert.Text &= Clipboard.GetText
           End If
           Me.Text_Convert.SelectionStart = Me.Text_Convert.Text.Length
      End If
End If

Unfortunately this only works if I use any letter or Function key - A in above the example - other than X, C, or V (the natural Windows Cut, Copy, Paste shortcuts). It's as though Windows itself is suppressing the paste?

I did take heed of your comment that this is the wrong code to use without adding:

VB.NET:
AndAlso Not e.Shift AndAlso Not e.Alt

to the If clause but when I run it only the Control Key combination works without adding the this extra code? (e.g., Alt+A does nothing.)

Thanks again for looking at this.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,457
Location
Sydney, Australia
Programming Experience
10+
when I run it only the Control Key combination works without adding the this extra code? (e.g., Alt+A does nothing.)
Let's check what I actually said:
That will trap the A key with any combination of modifiers that includes the Ctrl key.
Given that Alt+A doesn't include the Ctrl key, you wouldn't expect it to do anything. Ctrl+Alt+A would be a different story.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,457
Location
Sydney, Australia
Programming Experience
10+
I created a new project and added just what I needed to test. You should do the same. If you see what I saw then there's something different about your original project. If the new project works like your old project then there's something different about your system.
 

Chris M

Member
Joined
Feb 15, 2021
Messages
15
Programming Experience
Beginner
Ah, forgive me, I understand now your point now about Alt.

The problem does indeed seem to be a fault or setting with the Form I'm working on. I've just added a new form to the project with just a textbox on it and no added code. When I run this and just use the normal windows Ctrl+V in the textbox shortcut it works perfectly.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,540
Location
Norway
Programming Experience
10+
have also tried tried just naturally letting Windows Paste the text into the TextBox, with no added code in the KeyDown event, but still no joy.
Is the textbox ReadOnly?
 

Chris M

Member
Joined
Feb 15, 2021
Messages
15
Programming Experience
Beginner
No, ReadOnly is set to False.

I've checked every property on my original form against the newly added (test) form, then did the same for the textbox on each form. They are identical. The new form works perfectly well, the original does not (even when I add a new textbox to it).

I'm just going to have to rebuild the form it seems. Thankfully, it's relatively simple.

Thanks again for your help.
 

Chris M

Member
Joined
Feb 15, 2021
Messages
15
Programming Experience
Beginner
Just in case someone else runs up against the same problem, I found the culprit: I had a ToolStripMenuItem on the form with its ShortcutKeys set to Ctrl+V and it was obviously conflicting. I set the ToolStripMenuItem ShortcutKeys to 'none' and left the ShortcutKeyDisplayString as '(Ctrl+V)'. Problem solved.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,540
Location
Norway
Programming Experience
10+
If you want a Paste menu item, why not actually use it? When shortcut is pressed it's Click event is raised, where you can perform your custom paste. You may use ActiveControl to conditionally do that and regular TextBox.Paste if there are other textboxes.
 
Solution
Top Bottom