避免多次点击处理程序 [英] Avoid Multiple Click Handlers
本文介绍了避免多次点击处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有几个模拟键盘的按钮...好吧..每次单击一个键时,程序都必须重定向到其键的页面,好吗?...有比此键更好的方法吗?在每个click()事件上?
I have several buttons emulating a keyboard...well..everytime you click on a key, the program must redirect to the page of its key, ok?...Is there a better way of doing this than putting this on every click() event?
private void btnA_Click(object sender, EventArgs e)
{
ObtenerResultados(((Button) sender).Text);
}
private void btnB_Click(object sender, EventArgs e)
{
ObtenerResultados(((Button) sender).Text);
Application.DoEvents();
}
是否对所有按钮都调用相同的按钮...也许有更快的方法?
谢谢
Is the same call for all buttons...maybe there is a faster way?
Thanks
推荐答案
我并没有真正关注您,但是您的按钮可以共享相同的点击处理程序(您只需要一个"btn_Click",而不是"btnA_Click","btnB_Click"等).我不确定您使用的是ASP.Net还是Windows Forms(或WPF),因此我并不是很想为您提供可能会使您感到困惑的示例代码.基本上,请转到分配了单击处理程序的位置,然后选择其他选项.
I don''t really follow you, but your buttons can all share the same click handler (you only need a single "btn_Click" rather than a "btnA_Click", "btnB_Click", and so on). I''m not sure if you''re using ASP.Net or Windows Forms (or WPF), so I don''t really want to provide you with sample code that might confuse you. Basically, go to where the click handler is assigned and choose a different one.
为什么要从事件处理程序中调用DoEvents
?
只需创建一个按钮单击处理程序,然后做您已经在做的事情:
Why are you callingDoEvents
from an event handler?
Just create one button click handler and do what you''re already doing:
private void btnLetters_Click(object sender, EventArgs e)
{
ObtenerResultados(((Button) sender).Text);
}
然后将该事件附加到所有适用的按钮上.
And then attach that event to all of the applicable buttons.
好吧,更确切地说,您需要一个代表键盘的所有按钮的处理程序,但是您仍然需要有关确切按下按键的信息.另外,您还需要使用更好的语法-自动生成的语法已过时(您对v.4.0感兴趣,是吗?),并且相对于lambda语法而言相对较低.
该代码将是这样的:
Ok, more exactly, you need one handler for all buttons representing a keyboard, but you still need information on what exactly key was pressed. Also, you need to use better syntax -- the auto-generated syntax is gravely obsolete (you''re interested in v.4.0, huh?) and inferior relative the lambda syntax.
The code will be something like this:
for (Button button in KeyboardButtons)
//in the following line, you're sure sender can be cast to Button:
button.Click += (sender, args) => { ClickHandler((Button)sender); };
//...
//you don't have to use args parameter:
void ClickHandler(Button sender) {
string text = sender.Text; //in WPF it would be (string)sender.Content
Debug.Assert(text.Length > 0);
char key = text[1];
//now you know which key is represented by the button pressed
//...
} //ClickHandler
..//
这篇关于避免多次点击处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文