Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello experts,

I am a beginner to VB.NET but I could manage to get things done for small and simple programming.

What I will do in my project is that I will generate barcode and distribute it to the people. They are provided with a couple of barcode scanners and every person will have 3 barcodes to scan to complete one entry.
I could manage to generate barcode, now what I need is read the barcode and save data on my pc. I have plan to connect it with server, however for the time being I want to save it locally on my machine. Can you tell me how to do these.

For my more information can you also tell me,
If I buy a barcode scanner, do they provide any software to collect the read data, is that software customizable.
Could you please tell me how barcode reading works.

Please help me as this is my project and is required urgently.
Any help is highly appreciated.


Thanks in advance.
Posted
Updated 19-Jul-18 21:04pm

1 solution

If you buy barcode-scanners with an USB-connector, they will have keyboard-emulation. Meaning, they will send key-press-events to your application as if you entered something via keyboard. You can verify that by opening just any word-processing application and scanning a barcode. It should appear as if entered it via keyboard.

So all you would have to do is to ensure that some text-input-control has the input-focus before attempting to scan something and then process it like any text-input.

Theoretically there are ways to directly read it from the barcode-scanner so that you don't have to have that text-input-control but that would be way more advanced and, as you're saying you're a beginner to VB.NET, most probably out of your current scope. If you want to take a look at that anyway, I can serve you only with an article that does this with C#:
Using Raw Input from C# to handle multiple keyboards[^]

Edit after comment:
I built a sample-code for you. Create a new Windows-Forms-Project, add three TextBoxes to the Form via the Designer and name them BarcodeInput1, BarcodeInput2, BarcodeInput3 and add a Label named DemoLabel. Then replace the code of Form1 (source file "Form1.vb") with the following code.

If you enter (or scan) something in TextBox 1 and then press Enter (which usually barcode scanners automatically do after scanning a barcode), the entered/scanned Text will be displayed by DemoLabel and the input focus will automatically advance to TextBox 2, then to TextBox 3 and from there again to TextBox 1.

Please note that it's just a sample. I didn't clear the entered text from the TextBoxes. The VB.NET-code is an automatic translation from C# and may contain one or two oddities (but it's working).

VB.NET
VB
Public Class Form1
    Private TextBoxOrder As New Dictionary(Of TextBox, TextBox)()

    Public Sub New()
        InitializeComponent()

        TextBoxOrder.Add(BarcodeInput1, BarcodeInput2)
        TextBoxOrder.Add(BarcodeInput2, BarcodeInput3)
        TextBoxOrder.Add(BarcodeInput3, BarcodeInput1)

        BarcodeInput1.Tag = 1
        BarcodeInput2.Tag = 2
        BarcodeInput3.Tag = 3

        AddHandler BarcodeInput1.KeyDown, AddressOf BarcodeInputKeyDown
        AddHandler BarcodeInput2.KeyDown, AddressOf BarcodeInputKeyDown
        AddHandler BarcodeInput3.KeyDown, AddressOf BarcodeInputKeyDown

        AddHandler BarcodeInput1.Leave, AddressOf BarcodeInputLeave
        AddHandler BarcodeInput2.Leave, AddressOf BarcodeInputLeave
        AddHandler BarcodeInput3.Leave, AddressOf BarcodeInputLeave
    End Sub

    Private Sub BarcodeInputKeyDown(sender As Object, e As KeyEventArgs)
        If e.KeyCode = Keys.Enter AndAlso ActiveControl.[GetType]() = GetType(TextBox) Then
            Dim nextTextBox As TextBox
            If TextBoxOrder.TryGetValue(DirectCast(ActiveControl, TextBox), nextTextBox) Then
                e.Handled = True
                e.SuppressKeyPress = True
                nextTextBox.Focus()
            End If
        End If
    End Sub

    Private Sub BarcodeInputLeave(sender As Object, e As EventArgs)
        If sender.[GetType]() = GetType(TextBox) Then
            Dim textBox As TextBox = DirectCast(sender, TextBox)
            If textBox.Tag.[GetType]() = GetType(Integer) Then
                BarcodeScanned(textBox.Text, CInt(textBox.Tag))
            End If
        End If
    End Sub

    Private Sub BarcodeScanned(barcode As String, order As Integer)
        DemoLabel.Text = Convert.ToString(order.ToString() + ": ") & barcode
    End Sub
End Class


C#
C#
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BarcodeTest
{
    public partial class Form1 : Form
    {
        private Dictionary<TextBox, TextBox> TextBoxOrder = new Dictionary<TextBox, TextBox>();

        public BarcodeInputForm()
        {
            InitializeComponent();

            TextBoxOrder.Add(BarcodeInput1, BarcodeInput2);
            TextBoxOrder.Add(BarcodeInput2, BarcodeInput3);
            TextBoxOrder.Add(BarcodeInput3, BarcodeInput1);

            BarcodeInput1.Tag = 1;
            BarcodeInput2.Tag = 2;
            BarcodeInput3.Tag = 3;

            BarcodeInput1.KeyDown += BarcodeInputKeyDown;
            BarcodeInput2.KeyDown += BarcodeInputKeyDown;
            BarcodeInput3.KeyDown += BarcodeInputKeyDown;

            BarcodeInput1.Leave += BarcodeInputLeave;
            BarcodeInput2.Leave += BarcodeInputLeave;
            BarcodeInput3.Leave += BarcodeInputLeave;
        }

        private void BarcodeInputKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && ActiveControl.GetType() == typeof(TextBox))
            {
                TextBox nextTextBox;
                if(TextBoxOrder.TryGetValue((TextBox)ActiveControl, out nextTextBox))
                {
                    e.Handled = true;
                    e.SuppressKeyPress = true;
                    nextTextBox.Focus();
                }
            }
        }

        private void BarcodeInputLeave(object sender, EventArgs e)
        {
            if (sender.GetType() == typeof(TextBox))
            {
                TextBox textBox = (TextBox)sender;
                if (textBox.Tag.GetType() == typeof(int))
                {
                    BarcodeScanned(textBox.Text, (int)textBox.Tag);
                }
            }
        }

        private void BarcodeScanned(string barcode, int order)
        {
            DemoLabel.Text = order.ToString() + ": " + barcode;
        }
    }
}
 
Share this answer
 
v5
Comments
Code1000 19-Apr-15 16:40pm    
Thanks for your prompt reply, could you please explain it a bit more so that I can give it a try. I am using VS Express 2013. Suppose I have 3 textboxes (Textbox1, Textbox2,Textbox3) and I have 3 barcodes. When I scan these barcodes I want its data in each textboxes.
Thank for your time.
Sascha Lefèvre 19-Apr-15 17:20pm    
I added a sample code for you to my answer, please take a look. Please accept the solution if it was useful for you :-)
Code1000 19-Apr-15 18:05pm    
Thanks for your code and time however I think this is for C# but I need code for VB.NET please.
Sascha Lefèvre 19-Apr-15 18:18pm    
Sorry :-) I updated my answer above with the VB.NET-code.
Sascha Lefèvre 23-Apr-15 9:00am    
Did you check out the VB.NET code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900