连续引发两个事件 [英] Raising two events consecutively

查看:77
本文介绍了连续引发两个事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗体,其中除其他控件外,还包含一个按钮和一个文本框.当用户在文本框中输入内容时,随着用户离开文本框,我将使用"Leave"方法检查文本框的内容.如果在完成在文本框中的键入后,用户立即单击一个按钮,则将执行"Leave"事件的方法,但不会执行该按钮的Click事件的方法.一个简单的代码如下所示.如果键入textBox1并单击button1,则收到的唯一消息是"Hello from Textbox".

I have a form which among other controls has a button and a textbox. When a user types some stuff into the textbox, I examine the content of the textbox with the "Leave" method as the user moves away from the textbox. If upon finishing typing into the textbox the user clicks a button immediately, the method for the "Leave" event gets executed but the method for the button’s Click event does not. A trivial code is shown below. If you type into textBox1 and click on the button1, the only message you get is "Hello from Textbox".

private void textBox1_Leave(object sender, EventArgs e)
        {
            MessageBox.Show("Hello from Textbox");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello from button");
        }


感谢您对为何单击事件未执行的任何想法.

谢谢.
Gassim


I would appreciate any ideas on why the click event is not executing.

Thanks.
Gassim

推荐答案

在单击按钮之前,将禁用该窗体,以便可以在其上方显示模式MessageBox,并且范围从按钮更改为控件MessageBox.第二个事件永远都没有机会被提出.但是,很有可能连续有两个事件,如本示例所示(在具有两个TextBoxes的窗体上):
Before the button can be clicked, the form is disabled so that the modal MessageBox can be shown above it and the scope changes from the button to a control on the MessageBox. The second event never gets a chance to be raised. However, it is quite possible to have two events in a row, such as demonstrated by this example (on a form with two TextBoxes):
private void textBox1_Leave(object sender, EventArgs e)
{
	MessageBox.Show("Leave1");
}
private void textBox1_Enter(object sender, EventArgs e)
{
	MessageBox.Show("Enter1");
}
private void textBox2_Enter(object sender, EventArgs e)
{
	MessageBox.Show("Enter2");
}
private void textBox2_Leave(object sender, EventArgs e)
{
	MessageBox.Show("Leave2");
}


我怀疑这是Windows问题,按钮单击事件未触发,因为b/c文本框通常失去了对程序的关注,并且按钮不会被点击.
I would suspect that this is a windows issue, the button click event is not fired b/c the textbox is losing focus to the program in general, and the button is not clicked.


感谢您抽出宝贵的时间来回答Christian.看来我将不得不找到解决此问题的方法.
Thanks for taking the time to respond Christian. Looks like I am going to have to find a way around this problem.


这篇关于连续引发两个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆