Home > other >  Barcode Scan taking 1 character instead of a full set of String
Barcode Scan taking 1 character instead of a full set of String

Time:10-02

I am trying to make an app that scan barcodes from a usb scanner. The app is supposed to look for jobs corresponding the code from the barcode. My current problem is that when I try the app, it only takes 1 character and tries to look for the job corresponding that said character instead of allowing me to type an entire string and press enter.

Here is my code:

public void SearchJob(object sender, EventArgs e)
{
    this.ChangeCurrentEntity(this.Manager.GetEntity(j => j.Code == this.TypedView.BarcodeNumber)?.FirstOrDefault());
    
    this.LoadView();
    
    if (this.confirmation.GetConfirmation())
    {
        var tokenSource = new CancellationTokenSource();
        this.task = Task.Run(() => { }, tokenSource.Token);
    
        if (!task.IsCompleted)
            tokenSource.Cancel();

        if (dcConfig.CancelCode == "CancelCode")
        {
            this.Host.CloseHost();
        }
    }

    this.TypedView.BarcodeNumber = "";
}

getConfirmation() is a popUp to confirm the job, which is currently not relevant to my problem I think.

This is from my form:

private void JobNumberTextBox_TextChanged(object sender, EventArgs e)
{
    JobEntered?.Invoke(this, e);
}

Any help would be greatly appreciated.

CodePudding user response:

You are waiting on the wrong event. You need to wait on the KeyPress event

private void JobNumberTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if(e.KeyChar == (char)Keys.Return)
    {
        e.Handled = true;
        JobEntered?.Invoke(this, e);
    }
}

Side note: most barcode scanners can be configured to add an Enter key after a scan

CodePudding user response:

I think I solved my issue.

I removed my text changed function and replaced it with a Keydown function so that when I press enter the event to search for a job triggers(that way i am allowed to type a full string in). I also binded my Keydown with my TextBox.

private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                JobEntered?.Invoke(this, e);

                
            }
        }
  • Related