Can automatic press backspace when scanning barcodes in the textbox on the vb.net

162 Views Asked by At

Can automatic press backspace when scanning barcodes in the textbox on the vb.net?

is there any other solution or my code is wrong?

Thanks

 Private Sub txtUser_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtUser.KeyPress
 Const pattern As String = "USERNAME : (\S+)-PASSWORD : (\S+)"
  If e.KeyChar = ChrW(8) Then
        Dim m As Match = Regex.Match(txtUser.Text, pattern)
        If m.Success Then
  e.KeyChar = ControlChars.Back
            txtUser.Text = m.Groups(1).Value
            txtPassword.Text = m.Groups(2).Value
        Else
            'TODO: display some message
        End If
     End If
    End Sub

Result scanner in txtuser VIEW1

Result scanner after press backspace in keyboard. so I want immediate results without pressing backspace on the keyboard as below view2

1

There are 1 best solutions below

2
Antolin11 On

The barcode scanners add an enter at the end of the text. You can handle the keydown event instead of keypress to detect when Enter key is pressed and do something like that:

Private Sub txtUser_KeyPress(sender As Object, e As KeyEventArgs) Handles txtUser.KeyDown
    Const pattern As String = "USERNAME : (\S+)-PASSWORD : (\S+)"
    If e.KeyCode = Keys.Enter Then
        Dim m As Match = Regex.Match(txtUser.Text, pattern)
        If m.Success Then
            txtUser.Text = m.Groups(1).Value
            txtPassword.Text = m.Groups(2).Value
        Else
            'TODO: display some message
        End If
    End If
End Sub

With this code, you don't need to press the backspace key to split the barcode input into the two fields.

I hope that is helpful.