-
Jun 8th, 2025, 11:18 PM
#1
Thread Starter
Addicted Member
Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1.KeyDow
Hello
Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1.KeyDown Event
If messagebox is removed then the text pasted is Once which is what is required
I am trying to copy the Following text and pasting(Ctrl+v) or with (Shift +Insert) into Text via Clipboard into Textbox1
A well-written essay should be clear,
concise, and well-organized, with a strong introduction,
supporting paragraphs with evidence, and a concluding summary
Code:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.Control AndAlso e.KeyCode = Keys.V OrElse (e.Shift AndAlso e.KeyCode = Keys.Insert) Then
e.SuppressKeyPress = True
Dim originalText As String = Clipboard.GetText()
TextBox1.SelectedText = originalText
MessageBox.Show("Pasting the Whole Text....")
End If
End Sub
Result with above coding
A well-written essay should be clear,
concise, and well-organized, with a strong introduction,
supporting paragraphs with evidence, and a concluding summaryA well-written essay should be clear,
concise, and well-organized, with a strong introduction,
supporting paragraphs with evidence, and a concluding summary
Will really appreciate your help
Thanks
nkvb
Last edited by nkvb; Jun 8th, 2025 at 11:52 PM.
-
Jun 8th, 2025, 11:27 PM
#2
Re: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1,Ke
Looks like Windows is pasting once and you're setting the text yourself the other time...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 8th, 2025, 11:34 PM
#3
Thread Starter
Addicted Member
Re: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1,Ke
Looks like Windows is pasting once and you're setting the text yourself the other time...
Sir, I did not get you
Last edited by nkvb; Jun 8th, 2025 at 11:49 PM.
-
Jun 8th, 2025, 11:45 PM
#4
Thread Starter
Addicted Member
Re: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1,Ke
Code:
Dim isPasting As Boolean
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
isPasting =False
If e.Control AndAlso e.KeyCode = Keys.V OrElse (e.Shift AndAlso e.KeyCode = Keys.Insert) Then
e.SuppressKeyPress = True
Dim originalText As String = Clipboard.GetText()
TextBox1.SelectedText = originalText
MessageBox.Show("Pasting the Whole Text....")
End If
End Sub
Even incorporating the above isPasting As Boolean not helpful at all. Whether above the If and Then Condition or even in between the if Then condtion
nkvb
Last edited by nkvb; Jun 8th, 2025 at 11:54 PM.
-
Jun 9th, 2025, 12:09 AM
#5
Re: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1.Ke
There are a number of issues with your code. Firstly, there's no point your doing anything on the Ctrl+V key combination because Windows will already handle that. Secondly, the TextBox has a Paste method so, if you want to paste from the Clipboard, all you have to do is call that. I would also add that the way you're detecting Shift+Insert is bad because it won't differentiate between either, neither or both of the Ctrl and Alt keys being depressed. This is what your code should actually look like:
Code:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyData = (Keys.Shift Or Keys.Insert) Then
TextBox1.Paste()
End If
End Sub
That said, I'm seeing the same behaviour you describe even with that code. When pressing Ctrl+V, the Clipboard text is only inserted once but, when pressing Shift+Insert, it gets inserted twice. A breakpoint shows that Paste is only being called once, so that's not the issue. I have a vague feeling that I've encountered this before but, if so, I have no recollection of what the actual issue was or the solution.
-
Jun 9th, 2025, 12:16 AM
#6
Re: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1.Ke
Just did a little testing and, while I'm not sure exactly why it doesn't work otherwise, it seems that setting e.Handled fixes the issue. This worked for me:
Code:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyData = (Keys.Shift Or Keys.Insert) Then
TextBox1.Paste()
e.Handled = True
End If
End Sub
-
Jun 9th, 2025, 12:20 AM
#7
Re: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1.Ke
In case it's confusing you, this bit:
Code:
If e.KeyData = (Keys.Shift Or Keys.Insert) Then
is doing a bitwise combination of those two values, so it will detect when the Shift key and, importantly, ONLY the Shift key is pressed at the same time as the Insert key.
-
Jun 9th, 2025, 01:34 AM
#8
Thread Starter
Addicted Member
Re: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1.Ke
Jmc Sir,
Thank you so much for your valuable information with your coding
as per #5
Firstly, there's no point your doing anything on the Ctrl+V key combination because Windows will already handle that
Sir if not mistaken then Mostly and Widely used is Ctr+V to paste it and therfore it striked me
to use Ctr+V with option of Shift+insert
TextBox has a Paste method
Just learnt it from you
With your simple coding i come up with the following idea to remove the syntax
TextBox1.Paste() with CTRL+V as windows is controlling the same. Have incorporated the the message box
and using shift insert
Firstly, there's no point your doing anything on the Ctrl+V key combination because Windows will already handle that
Code:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyData = (Keys.Control Or Keys.V) Then
MessageBox.Show("Pasting the Whole Text....")
ElseIF e.KeyData = (Keys.Shift Or Keys.Insert) Then
Textbox1.Paste()
MessageBox.Show("Pasting the Whole Text....")
End If
End Sub
In both conditions msg box is displayed and Pasted Text is only Once.
I don't know how feasible is this for future encounters
thanks
nkvb
Last edited by nkvb; Jun 9th, 2025 at 01:39 AM.
-
Jun 14th, 2025, 01:22 AM
#9
Re: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1,Ke
I explained why it was appearing to paste twice.
 Originally Posted by .paul.
Looks like Windows is pasting once and you're setting the text yourself the other time...
Here's once...
 Originally Posted by jmcilhinney
Firstly, there's no point your doing anything on the Ctrl+V key combination because Windows will already handle that.
This is twice
 Originally Posted by nkvb
Code:
Dim originalText As String = Clipboard.GetText()
TextBox1.SelectedText = originalText
MessageBox.Show("Pasting the Whole Text....")
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 14th, 2025, 01:26 AM
#10
Re: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1.Ke
This should explain it better...
 Originally Posted by nkvb
Code:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyData = (Keys.Control Or Keys.V) Then
MessageBox.Show("Pasting the Whole Text....") ' Text pasted by system
ElseIF e.KeyData = (Keys.Shift Or Keys.Insert) Then
Textbox1.Paste() ' Pasting emulated by your code
MessageBox.Show("Pasting the Whole Text....")
End If
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|