GotFocus() 和 MDI 子项
我有一个带有 3 个子窗口的 MDI 表单。
MDI 开始为空。
每个子项都可以从菜单中打开一次。
当我打开多个窗口并关闭顶部窗口时,应该获得焦点,但新的活动子窗体的 GotFocus() 事件未触发。
Private Sub frmMain_gotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
...
End Sub
I have an MDI form with 3 child windows.
The MDI starts empty.
Each child can be opened once from the menu.
When I open multiple windows, and close the top one should get focus, but the new active child-form's GotFocus() event is not firing.
Private Sub frmMain_gotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
...
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的猜测。 Enter 或 Activated 事件都可以解决您的问题。
在 Windows 窗体编程中,您通常希望避免 GotFocus 和 LostFocus 事件。它们通常隐藏在设计器中,但并不始终如一。 Enter 和 Leave 事件分别是它们的替代品,它们是根据 UI 的逻辑状态而不是原始 Windows 消息生成的。使用验证和 MDI 时会有所不同。
激活是“自然”的,因为实际焦点移动到该形式的子控件。
Unlucky guess. Either the Enter or the Activated event will solve your problem.
In Windows Forms programming, you'd typically want to avoid the GotFocus and LostFocus events. They are often hidden in the designer, but not consistently. Respectively, the Enter and Leave events are their replacements, they are generated from the logical state of the UI instead of the raw Windows messages. Makes a difference when using Validating and MDI.
Activated is the "natural" one since the actual focus moves to a child control of that form.
当您从 MDI 父窗体创建 MDI 子窗体时,您应该将 MDI 父窗体订阅到 MDI 子窗体的 GotFocus 事件。当您的 MDI 子级发生任何焦点事件时,您的 MDI 父级会收到通知。
当关闭/打开窗体时,应自动选择 MDI Z 顺序中的下一个子窗体,并且如果您订阅该事件,您的 MDI 父窗体可以采取适当的操作并显示您的数据。
When you create the MDI children forms from the MDI parent, you should subscribe the MDI parent form to the MDI child form's GotFocus event. That when any focus events happen on your MDI Child, your MDI Parent gets notified.
When a form is closed/opened, the next child form in the MDI Z-Order should automatically be selected, and if you're subscribing to the event, your MDI parent can act appropriately and display your data.