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?

Dani AI

Generated

Good direction so far. Two things will make this reliable and fix the caret jump is seeing: (1) only inspect the text typed since the last newline so you do not react to embedded L2/L3/L4 (as warned), and (2) avoid rewriting the whole Text property, which resets the caret; append instead (building on ).

If this is WinForms:

Imports System.Text.RegularExpressions

Private inUpdate As Boolean = False

Private Sub txtTapeList_TextChanged(...) Handles txtTapeList.TextChanged
    If inUpdate Then Return
    Dim t = txtTapeList.Text
    Dim idx = t.LastIndexOf(Environment.NewLine, StringComparison.Ordinal)
    Dim segment = If(idx >= 0, t.Substring(idx + Environment.NewLine.Length), t)

    If Regex.IsMatch(segment, "L[234]\z", RegexOptions.IgnoreCase) Then
        inUpdate = True
        txtTapeList.AppendText(Environment.NewLine)   'keeps caret at end
        txtTapeList.SelectionStart = txtTapeList.TextLength
        txtTapeList.ScrollToCaret()
        inUpdate = False
    End If
End Sub

Notes:

  • AppendText avoids the caret jumping that happens when you replace .Text.
  • The inUpdate flag prevents recursive TextChanged calls.
  • Matching segment ensures 3234L423444L2 only triggers when the terminator is at the end of the current line.

If this is ASP.NET Web Forms (TextMode="MultiLine"), do it client-side to keep the caret and avoid postbacks:

<script>
(function () {
  var tb = document.getElementById('<%= txtTapeList.ClientID %>');
  if (!tb) return;
  tb.addEventListener('input', function () {
    var v = tb.value;
    var lastNL = v.lastIndexOf('\n');
    var seg = v.substring(lastNL + 1);
    if (/L[234]$/i.test(seg)) {
      tb.value = v + '\n';
      tb.selectionStart = tb.selectionEnd = tb.value.length;
    }
  });
})();
</script>

Tip: many scanners can append CR/LF or Tab as a suffix; enabling that makes the newline automatic and removes the need to parse L2/L3/L4 entirely.

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.
        Else
            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, networking, learning, and sharing knowledge.