C# WinForms:进入主菜单后不会触发文本框离开事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/331007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WinForms: Textbox Leave event doesn't get fired after going to main menu
提问by Grzenio
I have a TextBox control on my Form. I use the Leave event on the control to process user input. It works fine if the user clicks on some other control on the form, but the even doesn't get fired when the user goes straight to the main menu. Any ideas which event should I use to get it fired everytime?
我的表单上有一个 TextBox 控件。我使用控件上的 Leave 事件来处理用户输入。如果用户点击表单上的其他控件,它工作正常,但当用户直接进入主菜单时,甚至不会被触发。我应该使用哪个事件来每次触发它的任何想法?
采纳答案by Grzenio
I found a reasonable workaround, I set the focus on the main menu manually:
我找到了一个合理的解决方法,我手动将焦点设置在主菜单上:
EDIT: As suggested by @TcKs, I changed the event from ItemClicked to MenuActivate. Thanks very much for help!
编辑:正如@TcKs 所建议的,我将事件从 ItemClicked 更改为 MenuActivate。非常感谢您的帮助!
private void menuStrip1_MenuActivate( object sender, EventArgs e )
{
menuStrip1.Focus();
}
回答by TcKs
You should use "Validating" and "Validated" events for checking user's input. Then if user go to another control "A", and the control "A" has property "CausesValidation" set to "true" ( its default value ) the "Validating" and "Validated" event will be fired.
您应该使用“Validating”和“Validated”事件来检查用户的输入。然后,如果用户转到另一个控件“A”,并且控件“A”的属性“CausesValidation”设置为“true”(其默认值),则将触发“Validating”和“Validated”事件。
The menu has "CausesValidation" property too.
菜单也有“CausesValidation”属性。
Edit:Sorry, I forgot the "CausesValidation" in menu strip is our functionality and not built-in. But the check for validating is pretty simple:
编辑:对不起,我忘了菜单条中的“CausesValidation”是我们的功能而不是内置的。但是验证检查非常简单:
private void menuStrip1_MenuActivate( object sender, EventArgs e ) {
bool ret = this.Validate( false );
if ( false == ret ) {
// user's input is wrong
}
}
Use any ContainerControl instead of "this", if you want check the validation in another control than the "this" form. For example in MDI Child window.
如果您想在另一个控件而不是“this”表单中检查验证,请使用任何 ContainerControl 而不是“this”。例如在 MDI 子窗口中。
回答by Fredou
when playing with a menu, you have to simulate, somehow, a lostfocus and maybe remembering where it was in case you close the menu without doing anything so the focus would return into the textbox
在玩菜单时,您必须以某种方式模拟失去焦点,并且可能会记住它的位置,以防您在不执行任何操作的情况下关闭菜单,以便焦点返回到文本框
回答by Stefan
There are some cases when Lostfocus is not fired, for example clicking toolbar buttons and menu items. I use to work around this with a local "LastControl" variable and handle it myself when the menu got focus.
在某些情况下不会触发 Lostfocus,例如单击工具栏按钮和菜单项。我过去常常使用本地“LastControl”变量解决此问题,并在菜单获得焦点时自行处理。
There are reasons that menu click does not loses the textbox focus. If you want to have for example an "Edit" menu with "Paste" in, the "Paste" should act against the control that has focus and because of that it must not steal the focus from any controls on the form.
菜单单击不会失去文本框焦点是有原因的。例如,如果您想要一个带有“粘贴”的“编辑”菜单,则“粘贴”应该对具有焦点的控件起作用,因此它不能从窗体上的任何控件窃取焦点。
So the menu can be seen as a context menu that not steal the focus from the control.
所以菜单可以被看作是一个上下文菜单,不会从控件中窃取焦点。
回答by Fredou
you need a label 1 pixel per 1 pixel
你需要一个每 1 像素 1 像素的标签
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
MsgBox("yes")
End Sub
Private Sub MenuStrip1_MenuActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuStrip1.MenuActivate
CType(sender, MenuStrip).Tag = ActiveControl
Label1.Focus()
End Sub
Private Sub MenuStrip1_MenuDeactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuStrip1.MenuDeactivate
If CType(sender, MenuStrip).Tag Is Control AndAlso CType(CType(sender, MenuStrip).Tag, Control).CanFocus Then
CType(CType(sender, MenuStrip).Tag, Control).Focus()
End If
CType(sender, MenuStrip).Tag = Nothing
End Sub