如何根据两个标准以两种不同的方式触发同一事件? (C#)
我提出这个问题可能用词有误。这是一个棘手的提议,我迫切需要解决方案:-(
我想以两种不同的方式触发 textBox1_KeyDown
,但根据某些标准按下相同的键。下面的代码会更加清晰。
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (textBox1.Text == "")
{
if (e.KeyCode == Keys.Enter)
{
textBox1.Text = "x";
}
}
if (textBox1.Text != "")
{
if (e.KeyCode == Keys.Enter)
{
textBox1.Text = "y";
}
}
}
我想做的是,当我按 textBox1 上的 Enter 按钮时,如果其中没有文本,我希望它显示“x”,如果其中有一些文本,那么我希望文本框在按下时显示“y”。当我按照上面的方式进行编码时,这两个过程都会在一个实例中发生,也就是说,当我在 textBox1
为空时按 Enter,它会显示“y”(应该是“x”)。这意味着它首先显示“x”,然后由于文本框中有一个数量,因此文本变为“y”,如我的代码所调用的那样?就像我希望文本框仅显示“x”一样。 " 当它为空时,我按 Enter 键,或者当它不空白时,我按 Enter 键时,它应该只显示“y”。
我一定是错过了一些愚蠢的东西..谢谢..请给我代码。我几乎不懂技术术语..
I might have got the words wrong to put forward this question. This is a tricky proposition and I want solution urgently :-(
I want to trigger textBox1_KeyDown
in two different ways, but by keying down the same key based on some criteria. The code below would give more clarity.
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (textBox1.Text == "")
{
if (e.KeyCode == Keys.Enter)
{
textBox1.Text = "x";
}
}
if (textBox1.Text != "")
{
if (e.KeyCode == Keys.Enter)
{
textBox1.Text = "y";
}
}
}
What I'm trying to do is when I press Enter button on textBox1, I want it to display "x" if there's no text in it. If there's some text in it, then I want the textbox to display "y" upon pressing Enter. When I do the way I coded above, both the procedures happen in one instance. That's, when I press Enter when the textBox1
is blank it displays "y" (which should be "x"). That means it first displays "x" and then since the textbox has a quantity in it, the text becomes "y" as called by my code. How to Separate the two functions?? Like I want the textbox to show only "x" when its blank and I press Enter or it should show only "y" when its not blank and I press Enter.
I must be missing something silly.. Thanks.. Kindly gimme the code. I hardly understands technical terms..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是因为两个 if 语句都被执行了。第一个 if 语句执行并使文本框中的文本不为空。这也会导致下一个 if 语句被触发。只需这样做就可以修复它:
注意在第二个 if 语句上添加“else”。
That is because both if statements get executed. The first if statement executes and makes the text in the textbox not blank. This causes the next if statement to fire too. Simply doing this should fix it:
note addition of "else" on second if statement.
首先,您可以将
return;
放在最深嵌套的if
语句中,这样下一个if
就不会被执行。您可以做的另一件事是颠倒条件的顺序,因此您可以在处理程序的顶部而不是底部测试
if (textBox1.Text != "")
。最后,您可以在两个条件之间使用
else
。Well for a start you could put
return;
in your deepest-nestedif
statements so that the nextif
doesn't get executed.The other thing you can do is reverse the order of your conditions, so you test for
if (textBox1.Text != "")
at the top of your handler, rather than the bottom.And finally, you could just use
else
between the two conditionals.根据您在那里编写的内容,您可以翻转 if 语句的顺序或将第二个块设为 else 或 else if。
Based upon what you have written there you could just flip the order of your if statements or make the second block an else or else if.
也许我遗漏了一些东西,但为什么不添加一个 else:
Maybe I'm missing something, but why not just add an else:
您正在寻找类似的东西,所以我相信:
您也可以使用 三元运算符:
You're looking for something like this, so I believe:
You could also achieve this in a shorthanded fashion using the ternary operator: