C# 4.0 textbox为输入的每个数字发送enter键
我用一个文本框接受条形码输入,然后按enter键处理输入。但它会为输入的每个数字发送一次enter键。有没有办法只发送一次enter键?或者是压制对方进入C# 4.0 textbox为输入的每个数字发送enter键,c#-4.0,C# 4.0,我用一个文本框接受条形码输入,然后按enter键处理输入。但它会为输入的每个数字发送一次enter键。有没有办法只发送一次enter键?或者是压制对方进入 private void txtBscanned_KeyDown(object sender, KeyEventArgs e) { this.txtBscanned.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownHandler);
private void txtBscanned_KeyDown(object sender, KeyEventArgs e)
{
this.txtBscanned.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownHandler);
}
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(txtBscanned.Text);
}
}
为什么要在文本框本身上指定一个keydown事件?您应该在构造函数上或使用设计器分配事件
public Form1()
{
InitializeComponent();
txtBscanned.KeyDown += new KeyEventHandler(this.OnKeyDownHandler);
}
什么会触发你所说的“一次”呢?我输入13个数字,然后在最后输入。但是messagebox出现了13次。谢谢你修复了它。如果它修复了你的问题,请将其标记为答案