Results 1 to 10 of 10

Thread: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1.KeyDow

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2024
    Posts
    137

    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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    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...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2024
    Posts
    137

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2024
    Posts
    137

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,143

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,143

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,143

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2024
    Posts
    137

    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.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1,Ke

    I explained why it was appearing to paste twice.

    Quote Originally Posted by .paul. View Post
    Looks like Windows is pasting once and you're setting the text yourself the other time...
    Here's once...

    Quote Originally Posted by jmcilhinney View Post
    Firstly, there's no point your doing anything on the Ctrl+V key combination because Windows will already handle that.
    This is twice

    Quote Originally Posted by nkvb View Post
    Code:
    Dim originalText As String = Clipboard.GetText()
    TextBox1.SelectedText = originalText 
    MessageBox.Show("Pasting the Whole Text....")

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,185

    Re: Why does the textbox displays the data twice if MsgBox incorpoated in TextBox1.Ke

    This should explain it better...

    Quote Originally Posted by nkvb View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width