如何从WinForms应用程序中的用户控件的子控件中获取事件? [英] How do I grab events from sub-controls on a user-control in a WinForms App?
问题描述
我有一个自定义的用户控件,可以让主窗体拦截在用户控件上的子控件上触发的事件吗?嵌入我的应用程序的主窗体。控件包含操纵数据的各种子控件,其本身由主窗体上的其他控件显示。我想要的是,如果主窗体可以以某种方式通知用户更改子控件时,所以我可以更新数据和其他地方的相应显示。
现在,我在作弊我有一个代表连接到子控件的焦点离开事件。该委托更改了我不在其他地方使用的用户控件的属性(在此原因中,CausesValidation)。然后我在主窗体上定义了一个委托,用于当用户控件的CausesValidation属性更改时,然后指示应用程序更新数据并显示。
一个问题因为我也有一个委托设置焦点离开用户控制,因为我需要验证用户控件中的字段之前,我可以允许用户做任何事情。但是,如果用户只是在子控制之间进行切换,我不想验证,因为它们可能无法进行编辑。
基本上,我希望数据更新当用户切换子控制或离开用户控制,但不能验证。当用户离开控件时,我想更新AND验证。现在,离开用户控制会导致验证触发两次。
最佳做法是在 UserControl
将事件弹起到父表单。我已经走了,为你组建了一个例子。以下是此示例提供的描述。
-
UserControl1
- 创建一个
UserControl
与TextBox1
- 在
UserControl
上注册公开活动,名为ControlChanged
- 在
UserControl
中注册一个事件处理程序TextBox1
TextChangedEvent - 在
TextChangeEvent
处理程序函数中,我将调用ControlChanged
事件来浮动到父表单
- 创建一个
-
Form1
- 删除一个实例在设计器上的
UserControl1
- 在
UserControl1
上注册一个事件处理程序, code> MouseLeave 和ControlChanged
- 删除一个实例在设计器上的
这是一个屏幕截图,说明我在 UserControl中定义的
ControlChanged
事件, code>可以通过父窗口的Visual Studio中的UX使用。
用户控件的事件处理程序http://friendfeed.s3.amazonaws.com/0d5a3968cb785625c8afb3974a8d84894c476291
Is there any way for the main form to be able to intercept events firing on a subcontrol on a user control?
I've got a custom user-control embedded in the main Form of my application. The control contains various subcontrols that manipulate data, which itself is displayed by other controls on the main form. What I'd like is if the main form could be somehow informed when the user changes subcontrols, so I could update the data and the corresponding display elsewhere.
Right now, I am cheating. I have a delegate hooked up to the focus-leaving event of the subcontrols. This delegate changes a property of the user-control I'm not using elsewhere (in this cause, CausesValidation). I then have a delegate defined on the main form for when the CausesValidation property of the user control changes, which then directs the app to update the data and display.
A problem arises because I also have a delegate set up for when focus leaves the user-control, because I need to validate the fields in the user-control before I can allow the user to do anything else. However, if the user is just switching between subcontrols, I don't want to validate, because they might not be done editing.
Basically, I want the data to update when the user switches subcontrols OR leaves the user control, but not validate. When the user leaves the control, I want to update AND validate. Right now, leaving the user-control causes validation to fire twice.
The best practice would be to expose events on the UserControl
that bubble the events up to the parent form. I have gone ahead and put together an example for you. Here is a description of what this example provides.
UserControl1
- Create a
UserControl
withTextBox1
- Register a public event on the
UserControl
calledControlChanged
- Within the
UserControl
register an event handler for theTextBox1
TextChangedEvent - Within the
TextChangeEvent
handler function I call theControlChanged
event to bubble to the parent form
- Create a
Form1
- Drop an instance of
UserControl1
on the designer - Register an event handler on
UserControl1
forMouseLeave
and forControlChanged
- Drop an instance of
Here is a screenshot illustrating that the ControlChanged
event that I defined on the UserControl
is available through the UX in Visual Studio on the parent Windows form.
这篇关于如何从WinForms应用程序中的用户控件的子控件中获取事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!