我写了这段代码,我想用条形码阅读器控制输入到文本框中:
private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.P && e.KeyCode != Keys.V && e.KeyCode != Keys.Enter && e.KeyCode != Keys.Back)
{
MessageBox.Show("Wrong value!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox3.Text = textBox3.Text.Remove(textBox3.Text.LastIndexOf(Environment.NewLine));
textBox3.Focus();
textBox3.SelectionStart = textBox3.Text.Length;
textBox3.AppendText(Environment.NewLine);
}
if (e.KeyCode == Keys.Enter)
{
textBox1.Focus();
textBox3.AppendText(Environment.NewLine);
textBox3.ReadOnly = true;
textBox1.ReadOnly = false;
}
}
在这个文本框中我只想要字母“P”或“V”,直到我用键盘测试这一切都很好。 今天我连接了条形码阅读器,现在当我扫描“P”或“V”条形码时,它给了我错误。
我应该使用其他文本框事件吗?这有什么问题? 欢迎任何帮助,谢谢。
工作版本:
private void textBox3_KeyDown(object sender,KeyEventArgs e) {
if (e.KeyCode != Keys.P && e.KeyCode != Keys.V)
{
MessageBox.Show("Wrong value!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox3.Text = textBox3.Text.Remove(textBox3.Text.LastIndexOf(Environment.NewLine));
textBox3.Focus();
textBox3.SelectionStart = textBox3.Text.Length;
textBox3.AppendText(Environment.NewLine);
}
if (e.KeyCode == Keys.V)
{
textBox1.Focus();
textBox3.AppendText("v");
textBox3.AppendText(Environment.NewLine);
textBox3.ReadOnly = true;
textBox1.ReadOnly = false;
}
if (e.KeyCode == Keys.P)
{
textBox1.Focus();
textBox3.AppendText("p");
textBox3.AppendText(Environment.NewLine);
textBox3.ReadOnly = true;
textBox1.ReadOnly = false;
}
}