I have a multiline text box which is used to enter barcode numbers into. I have a barcode scanner and all the barcodes will end in L2, L3 or L4. Once a barcode is scanned in I need VB to detect either L2, L3 or L4 then go to a new line and repeat.

Any ideas on where I can start to accomplish this?

Recommended Answers

All 4 Replies

You can attach to the TextChanged event and check if the last 2 characters in the text string are L2, L3 or L4 and add a newline to the textbox. An issue you might encounter is if the barcode has an imbedded L2, L3, L4 value (such as 3234L423444L2).

See if this helps.

'// code to get Scanned BarCode here.
        If Not TextBox1.Lines.Length = 0 Then '// check if TextBox contains text.
            TextBox1.Text &= vbNewLine & "my Scanned BarCode" '// add new line and BarCode.
            TextBox1.Text = "my Scanned BarCode" '// add BarCode.
        End If

With the above code, you should not have any "empty" lines in your TextBox.

What I had so far is this:

If txtTapeList.Text.EndsWith("L3") Then
            txtTapeList.Text += vbNewLine
        End If

What happens is when I scan (or type) a barcode in the text box the cursor jumps to the begining of the line. If you press the down arror key the cursor does move to the new line but it won't do it automatically for some reason.

Since you are not adding numbers, but "adding to something", use the & character instead.
txtTapeList.Text &= vbNewLine

Final code with setting the cursor to a preset location.

If txtTapeList.Text.EndsWith("L3") Then
            txtTapeList.Text &= vbNewLine
            txtTapeList.Select(txtTapeList.TextLength, 1)'// "txtTapeList.TextLength" = start Index.
        End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.