我有一个注册系统,人们扫描条形码并将其记录下来。条形码在'*'后面有一个唯一的数字。当他们扫描他们的代码时,它进入列表框并清除文本框中的剩余部分,但是,它没有清除'*'。
有什么想法吗?以下是代码:
private void textBox1_KeyUp(object sender, KeyPressEventArgs e)
{
Object returnValue;
string txtend = textBox1.Text;
returnValue = textBox1.Text.Replace(@"*", "");
if (e.KeyChar != '*') return;
{
if (listBox1.Items.Contains(returnValue))
{
for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
string removelistitem = returnValue.ToString();
if (listBox1.Items[n].ToString().Contains(removelistitem))
{
//listBox1.Items.RemoveAt(n);
}
}
}
else
listBox1.Items.Add(returnValue);
textBox1.Clear();
System.IO.StreamWriter sw = new System.IO.StreamWriter(fullFileName);
foreach (object item in listBox1.Items)
sw.WriteLine(item.ToString());
sw.Flush();
sw.Close();
if (listBox1.Items.Count != 0) { DisableCloseButton(); }
else
{
EnableCloseButton();
}
label6.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance.";
}
}
答案 0 :(得分:2)
这一行
if (e.KeyChar != '*') return;
当按下的键不是'*'时,提前结束事件处理。使用以下{}块,它看起来像旧的/测试残骸。
在第一个else
- Block之后,if
也可能存在一些问题。我建议始终使用{},即使块中只有一行。
答案 1 :(得分:2)
在重新阅读您的问题后,我了解到在处理完'*'后,它被插入到空文本框中。
要停止处理key up事件,请使用以下语句:
e.Handled = true;
这表明该事件由您的代码处理,不应进一步处理。