简体   繁体   English

手动引发控件上的LostFocus事件

[英]Raise LostFocus event on a control manually

I have a bunch of controls (textbox and combobox) on a form with toolstripcontainer and toolstripbuttons for save, cancel etc for edits. 我在窗体上有一堆控件(文本框和组合框),其中带有toolstripcontainer和toolstripbuttons用于保存,取消等用于编辑。 We are using .Net 3.5 SP1 我们正在使用.Net 3.5 SP1
There is bunch of logic written in control.lostfocus and control.leave events. 在control.lostfocus和control.leave事件中编写了大量逻辑。 These events are not being called when clicked on the toolstrip buttons. 单击工具栏按钮时不会调用这些事件。 Is there a way to call these events manually when any of these buttons are pressed. 当按下这些按钮中的任何一个时,是否可以手动调用这些事件。

Thanks. 谢谢。
Kishore Kishore

[Edit] [编辑]

This is how I solved the problem. 这就是我解决问题的方式。 Thanks Chris Marasti-Georg for the pointer. 感谢Chris Marasti-Georg的指导。 In the button click event I calling focus on the toolstrip instead of the button as the toolstripbutton does not have a focus event. 在按钮单击事件中,由于工具条按钮没有焦点事件,因此将焦点称为工具条而不是按钮。 We can access the toolstrip on which the button is placed using 我们可以使用来访问放置按钮的工具条

((ToolStripButton)sender).Owner.Focus() [[ToolStripButton)sender).Owner.Focus()

-Kishore -基肖尔

You could listen to the click events on the buttons, and in the handler call their focus method. 您可以监听按钮上的click事件,然后在处理程序中调用它们的focus方法。 That would (hopefully) cause the previously focused control to respond correctly. 这将(希望)导致先前集中的控件正确响应。 Add the following handler to each button's click event: 将以下处理程序添加到每个按钮的click事件中:

private void ButtonClick(object sender, EventArgs e) {
    if(sender != null) {
        sender.Focus();
    }
}

您可以扩展这些控件,然后调用基类的OnLostFocus和OnLeave受保护的方法...

我建议将登录名移至事件处理程序之外的方法,然后调用该方法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM