locked
Detect Ctrl+V RRS feed

  • Question

  • Hey all

    I limit allowed characters in a text box using KeyPress & TextChanged, here's my TextChanged part:

    Dim A, B As Integer
    Dim FileNameFilter As String = String.Empty
    For A = 0 To MessageTextBoxX.TextLength - 1
        B = Convert.ToInt32(MessageTextBoxX.Text.Chars(A))
        Select Case B
            'Allow Dot and Numbers
            Case 46, 48 To 57
                'OK. Do nothing.
                FileNameFilter = FileNameFilter + MessageTextBoxX.Text.Chars(A)
            Case Else
                'Not good.
        End Select
    Next
    MessageTextBoxX.Text = FileNameFilter

    How to add Ctrl+V support?

    Tuesday, July 28, 2020 11:24 AM

Answers

  • Even then it works for me. It's time for you to post a concise and complete example. Thus craft a new form, which shows this behavior.

    btw, you should check the content once, not twice. What's the reason for this? And it should look something like this

    Private Function ValidateNumbers(ATextBox As TextBox, AAcceptValidCharsOnly As Boolean) As Boolean
    	Dim Index, CurrentChar As Integer
    	Dim ValidText As String = String.Empty
    	Dim InputText As String = ATextBox.Text
    
    	ValidateNumbers = False
    	For Index = 0 To InputText.Length - 1
    		CurrentChar = Convert.ToInt32(InputText.Chars(Index))
    		Select Case CurrentChar
    			Case 46, 48 To 57
    				ValidateNumbers = True
    				ValidText = ValidText + InputText.Chars(Index)
    		End Select
    	Next
    
    	If AAcceptValidCharsOnly And Not ATextBox.Text.Equals(ValidText) Then
    		ATextBox.Text = ValidText
    	End If
    End Function
    
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    	ValidateNumbers(DirectCast(sender, TextBox), True)
    End Sub
    
    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    	ValidateNumbers(DirectCast(sender, TextBox), True)
    End Sub

    The above code works here..

    • Marked as answer by OSVBNET Tuesday, July 28, 2020 6:19 PM
    Tuesday, July 28, 2020 1:54 PM
  • Firstly, i would recommend looking into the TextBox.MaxLength property

    However, if that is not an option: It is not elegant, but...

     

    Private controlKeyIsDown As Boolean = False
      Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = 17 Then ' 17 is the left or right control key
          controlKeyIsDown = True
        ElseIf e.KeyCode = Keys.V Then
          If controlKeyIsDown Then
            e.SuppressKeyPress = True ' or do any other validation you want
          End If
        End If
      End Sub
      Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
        If e.KeyCode = 17 Then
          controlKeyIsDown = False
        End If
      End Sub

    Hope that helps



    • Edited by BNIinc Tuesday, July 28, 2020 2:05 PM
    • Marked as answer by OSVBNET Tuesday, July 28, 2020 6:19 PM
    Tuesday, July 28, 2020 1:58 PM
  • Sure:

    Private controlKeyIsDown As Boolean = False Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = 17 Then ' 17 is the left or right control key controlKeyIsDown = True ElseIf e.KeyCode = Keys.V Then If controlKeyIsDown Then If textIsValidForTextbox(textbox1.text + Clipboard.getText()) textbox1.text = textbox1.text + Clipboard.getText() else e.SuppressKeyPress = True ' because your validation failed End If End If End Sub Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp If e.KeyCode = 17 Then controlKeyIsDown = False End If End Sub

    private function textIsValidForTextbox(text as string) as boolean if <boolean expression to determine if this is invalid> then return false else if <boolean expression #2 to determine if this is invalid> return false … … … end if return true End Sub


    Hope this helps





    • Edited by BNIinc Tuesday, July 28, 2020 7:13 PM Partially in C#, not VB :)
    • Marked as answer by OSVBNET Tuesday, July 28, 2020 7:29 PM
    Tuesday, July 28, 2020 7:08 PM

All replies

  • Please rephrase your question. Cause it is not clear what you mean.

    The TextBox control supports copy'n'paste per default. For validating controls use the change event of the involved control(s).

    Tuesday, July 28, 2020 11:54 AM
  • Thanks mate, when you paste the above code in TextChanged event of TextBox all keyboard shortcuts are disabled and you must manually add them in Case...

    I just add dot and numbers, and can't figure out how to add Ctrl+V support there, is there an ASCII code for Ctrl+V?

    Tuesday, July 28, 2020 11:57 AM
  • hmm, sure?

    Cause this works on an new Form:

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    	Dim A, B As Integer
    	Dim FileNameFilter As String = String.Empty
    	Dim text As String = DirectCast(sender, TextBox).Text
    	For A = 0 To text.Length - 1
    		B = Convert.ToInt32(text.Chars(A))
    		Select Case B                   'Allow Dot and Numbers
    			Case 46, 48 To 57           'OK. Do nothing.
    				FileNameFilter = FileNameFilter + text.Chars(A)
    			Case Else                   'Not good.
    		End Select
    	Next
    	DirectCast(sender, TextBox).Text = FileNameFilter
    End Sub

    Tuesday, July 28, 2020 12:39 PM
  • Yeah sure, I mentioned that I use both KeyPress & TextChanged

    KeyPress for pressing single keys, TextChanged for pasting contents

    And the same check is inside both preventing Ctrl+V

    Tuesday, July 28, 2020 12:47 PM
  • Even then it works for me. It's time for you to post a concise and complete example. Thus craft a new form, which shows this behavior.

    btw, you should check the content once, not twice. What's the reason for this? And it should look something like this

    Private Function ValidateNumbers(ATextBox As TextBox, AAcceptValidCharsOnly As Boolean) As Boolean
    	Dim Index, CurrentChar As Integer
    	Dim ValidText As String = String.Empty
    	Dim InputText As String = ATextBox.Text
    
    	ValidateNumbers = False
    	For Index = 0 To InputText.Length - 1
    		CurrentChar = Convert.ToInt32(InputText.Chars(Index))
    		Select Case CurrentChar
    			Case 46, 48 To 57
    				ValidateNumbers = True
    				ValidText = ValidText + InputText.Chars(Index)
    		End Select
    	Next
    
    	If AAcceptValidCharsOnly And Not ATextBox.Text.Equals(ValidText) Then
    		ATextBox.Text = ValidText
    	End If
    End Function
    
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    	ValidateNumbers(DirectCast(sender, TextBox), True)
    End Sub
    
    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    	ValidateNumbers(DirectCast(sender, TextBox), True)
    End Sub

    The above code works here..

    • Marked as answer by OSVBNET Tuesday, July 28, 2020 6:19 PM
    Tuesday, July 28, 2020 1:54 PM
  • Firstly, i would recommend looking into the TextBox.MaxLength property

    However, if that is not an option: It is not elegant, but...

     

    Private controlKeyIsDown As Boolean = False
      Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = 17 Then ' 17 is the left or right control key
          controlKeyIsDown = True
        ElseIf e.KeyCode = Keys.V Then
          If controlKeyIsDown Then
            e.SuppressKeyPress = True ' or do any other validation you want
          End If
        End If
      End Sub
      Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
        If e.KeyCode = 17 Then
          controlKeyIsDown = False
        End If
      End Sub

    Hope that helps



    • Edited by BNIinc Tuesday, July 28, 2020 2:05 PM
    • Marked as answer by OSVBNET Tuesday, July 28, 2020 6:19 PM
    Tuesday, July 28, 2020 1:58 PM
  • @BNIinc: Could you reproduce a TextBox where Ctrl-V failed with those validation active? How?

    Tuesday, July 28, 2020 2:09 PM
  • @BNIinc: Could you reproduce a TextBox where Ctrl-V failed with those validation active? How?

    I also use:

    MessageTextBoxX.ContextMenu = New System.Windows.Forms.ContextMenu

    To disable right click for some reason so only way would be Ctrl+V

    *** you were right it was not needed to double check it, my bad logic :)
    • Edited by OSVBNET Tuesday, July 28, 2020 6:20 PM
    Tuesday, July 28, 2020 6:07 PM
  • Sure:

    Private controlKeyIsDown As Boolean = False Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = 17 Then ' 17 is the left or right control key controlKeyIsDown = True ElseIf e.KeyCode = Keys.V Then If controlKeyIsDown Then If textIsValidForTextbox(textbox1.text + Clipboard.getText()) textbox1.text = textbox1.text + Clipboard.getText() else e.SuppressKeyPress = True ' because your validation failed End If End If End Sub Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp If e.KeyCode = 17 Then controlKeyIsDown = False End If End Sub

    private function textIsValidForTextbox(text as string) as boolean if <boolean expression to determine if this is invalid> then return false else if <boolean expression #2 to determine if this is invalid> return false … … … end if return true End Sub


    Hope this helps





    • Edited by BNIinc Tuesday, July 28, 2020 7:13 PM Partially in C#, not VB :)
    • Marked as answer by OSVBNET Tuesday, July 28, 2020 7:29 PM
    Tuesday, July 28, 2020 7:08 PM