Search code examples
c#barcode

Auto clear textbox after barcode input and passing the value to a string


this is my first time to use this barcode reader hardware. Im having a problem in auto clear the textbox after barcode input. It clears the textbox before it passes the value to a respective string.

private void txtStudentID_TextChanged(object sender, EventArgs e)
{
    string a = txtStudentID.Text;
    sqlDisplayInfo = a;
    txtStudentID.Text.Clear()
}

I also tried using thread but it fails

private void txtStudentID_TextChanged(object sender, EventArgs e)
{
    string a = txtStudentID.Text;
    sqlDisplayInfo = a;
    Thread.Sleep(1000);
    txtStudentID.Text.Clear()
}

EDIT for my new code:

i have problem here +=. example i scanned a barcode then it combines the previous scan and the next scan

i use timer tick but it is not perfect as it is because sometimes it cause delay in retrieving the data in the database, so it does not collaborate with the time intervals

private void txtStudentID_TextChanged(object sender, EventArgs e)
        {
            timer1.Interval = (700);
            timer1.Enabled = true;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            a = txtStudentID.Text;
            displayData(a);//passing string a to method data retrieve
            timer2.Interval = (700);
            timer1.Enabled = true;
            timer2.Start();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(this.txtStudentID.Text))
            {
                timer2.Stop();
                txtStudentID.Clear();
            }
        }

Solution

  • Thanks to all for giving ideas. Now i figure it out..

    private void txtStudentID_TextChanged(object sender, EventArgs e)
            {
                timer1.Interval = (700);
                timer1.Enabled = true;
                timer1.Start();
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                timer1.Stop();
                a = txtStudentID.Text;
                displayData(a);// i put the txtStudentID.Clear() at the end of the method. I think i need some rest. :)
            }