检测用户控件何时获得焦点的事件?
我的表单上有一个用户控件,可以生成多个非模式子表单。子窗体显示用户可以编辑的数据。用户控件显示相同数据的不同视图,并且如果用户在一个或多个子表单中进行了更改,我希望能够重新加载数据。我已经设置了事件来通知用户控件子窗体中的数据更改,但我无法弄清楚应该在用户控件上捕获哪个事件(如果存在)以确定它已从子表格。我可以在这里使用现有的事件吗?
编辑:代码示例 (抱歉,我的客户喜欢 VB)
GotFocus 似乎仅在 UserControl 被处置时才会触发。
我将控件添加到“主”窗体上的面板,如下所示:
Dim mainControl as New MainUserControl()
Panel1.Controls.Add(mainControl)
mainControl.Doc = DocStyle.Fill
mainControl.Visible = True
mainControl.Show()
然后,事件处理程序代码:
Private Sub MainUserControl_GotFocus(ByVal sender as Object, ByVal e as EventArgs) Handles Me.GotFocus
MessageBox.Show("got focus")
End Sub
然后,用户控件上有一个“关闭”按钮,该按钮将事件触发回主窗体,然后删除用户从面板控制并处理它。仅当调用 mainControl.Dispose()
时,GotFocus
才会触发。
编辑2
我刚刚尝试处理 Enter
事件,但这仅在第一次触发,而不是每次控件接收焦点时触发。
I have a user control on a form that can spawn several non-modal child forms. The child forms display data that the user can edit. The user control displays a different view of the same data, and I want to be able to spawn a reload of the data if the user has made changes in one or more of the child forms. I already have events set up to notify the user control of data changes in the child forms, but I can't figure out which event (if it exists) I should capture on the user control to determine that it has gotten focus back from the child form(s). Is there an existing event I can use here?
EDIT: Code sample
(sorry, my client likes VB)
GotFocus only seems to fire when the UserControl is Disposed.
I add the control to a panel on the "main" form like this:
Dim mainControl as New MainUserControl()
Panel1.Controls.Add(mainControl)
mainControl.Doc = DocStyle.Fill
mainControl.Visible = True
mainControl.Show()
Then, the event handler code:
Private Sub MainUserControl_GotFocus(ByVal sender as Object, ByVal e as EventArgs) Handles Me.GotFocus
MessageBox.Show("got focus")
End Sub
Then, there's a "close" button on the user control that fires an event back to the main form, which then removes the user control from the panel and disposes it. Only when mainControl.Dispose()
is called does GotFocus
fire.
EDIT 2
I just tried handling the Enter
event, but that only fires the first time and not every time the control receives focus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能找到的最好方法是在用户控件中处理父窗体的
Activated
事件。从过去几个小时所做的所有研究中,我了解到GotFocus
使用起来很棘手,而且 Winforms 用户控件根本不喜欢获得焦点,因为它是一个容器控件。假设我有一个用户控件MyUserControl
加载到名为“MyControlParentForm”的表单上,那么我使用的代码如下所示:然后,如果表单因任何原因失去焦点然后重新获得焦点
MyUserControl
通过Control_Activated
了解它。希望这对将来的人有帮助。The best way to do this that I can find is by handling the parent form's
Activated
event in the user control. From all the research I've done over the last few hours, I've learned thatGotFocus
is a tricky thing to work with, and that the Winforms user control doesn't like to have focus at all, because it's a container control. Say I have a user controlMyUserControl
that is loaded onto a form called 'MyControlParentForm', then the code I'm using is something like this:Then, if the form loses focus for any reason and then regains it,
MyUserControl
knows about it throughControl_Activated
. Hopefully this helps someone in the future.c# 中有一个事件称为 Control.GotFocus Event。在.net 2.0之前不存在。
There is an event in c# called Control.GotFocus Event. does not exist before .net 2.0.
我就是这样骗人的
I trick like this.