hasni
hasni

Reputation: 29

How to read an input from a barcode scanner and display into a TextBox?

First barcode content will be: 1|123456|ABC

Second barcode content will be: 2|123456789542|ABCDSE

I just want to retrieve the bold part.

Here's my code:

Private Sub TextScanPartNo_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextScanPartNo.KeyPress
    TextVendorID.CharacterCasing = CharacterCasing.Upper
    If Me.TextVendorID.Focused = False Then
        TextVendorID.Focus()
        TextVendorID.Text = e.KeyChar.ToString     
        e.Handled = True
    End If
End Sub

Upvotes: 0

Views: 1801

Answers (2)

Bugs
Bugs

Reputation: 4489

If the input is always going to be xxx|xxxxxx|xxx then this is quite easily done. We can use the String.Split method:

Dim textToSplit As String = "1|123456|abc"

Dim text As String = textToSplit.Split(New Char() {"|"c})(1)

Note the use of (1) at the end of .Split(). .Split() returns a String() which results in the following output:

(0) 1
(1) 123456
(2) abc

Since you only want 123456 we can target this by appending (1) to the end of the .Split method.

Output of text is:

123456

Edited as OP has stated they are having problems with input "2|123456789542|ABCDSE". The format still stands as being xxx|xxxxxx|xxx so the Split() code will still work.

This is a screenshot of the code showing the output you are after:

enter image description here

Upvotes: 2

XMozart
XMozart

Reputation: 929

You can use the split function:

Dim str() As String
str = Split(barcodeText, "|")
MsgBox("Your barcode is : " & str(1))

Upvotes: 1

Related Questions