条形码输入后自动清除文本框并将值传递给字符串

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

这是我第一次使用这个条码 reader 硬件。我在输入条形码后自动清除文本框时遇到问题。它在将值传递给相应的字符串之前清除文本框。

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

我也尝试过使用线程,但失败了

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

编辑我的新代码:

我这里有问题+=。例如,我扫描了一个条形码,然后它结合了上一次扫描和下一次扫描

我使用定时器滴答,但它并不完美,因为有时它会导致在数据库中检索数据的延迟,因此它不与时间间隔协作

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();
            }
        }

我想问题不在于条形码 reader,而是您正在使用文本更改事件这一事实。它在 reader 向文本框提供值时触发(我假设这就是文本框的填充方式)。但它也会在您清除文本框时触发。

那么会发生什么:

  1. 读取值(例如“123”):填充文本框。变量 sqlDisplayInfo 填充为“123”。
  2. 方法的下一行清除文本框。
  3. 文本框是空的(所以文本确实改变了)。 textChanged 事件被触发。
  4. 文本框文本为空字符串。因此相同的值被分配给 sqlDisplayInfo.

因此您看不到 sqlDisplayInfo 的值有任何变化。尝试调试您的代码,然后您会看到该事件被触发了两次。

编辑:

从您的评论看来,条形码 reader 是一个字母一个字母地读取的。所以理想情况下会有一个信号表明读数已完成。您读取字符串,在临时变量中构建它,当收到信号时,您将临时变量的值分配给您的 sqlDisplayInfo,清除变量和文本框。

另一种选择可能只是读取文本框并将其附加(而不是赋值)到 sqlDisplayInfo 变量。

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (string.IsNullOrWhiteSpace(this.textBox1.Text))
    {
        sqlDisplayInfo += this.textBox1.Text;
    }

    this.textBox1.Clear();
}

您仍会触发两倍于必要的文本更改事件,但仅在您读取值后文本框发生更改时才会做出反应。

感谢大家提供意见。 现在我明白了..

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. :)
        }

这可能对你有帮助!

    private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
            {
                try
                {
                    if (e.KeyChar == (char)Keys.Enter)
                    {
                        if (!string.IsNullOrEmpty(txtSearch.Text))
                        {
                            var itemKey = _blItems.GetItemKeyByBarcode(txtSearch.Text);
                            if (itemKey > 0)
                            {
                                var model = _blItems.GetItemDetailsDataTable(itemKey);
                                dataGridView1.AutoGenerateColumns = false;
                                dataGridView1.DataSource = model;
                            }
                            txtSearch.Clear();
                            txtSearch.Select();
                        }
                    }
                }
                catch { }
            }