如何从 WinForms 应用程序中用户控件上的子控件中获取事件?

How do I grab events from sub-controls on a user-control in a WinForms App?(如何从 WinForms 应用程序中用户控件上的子控件中获取事件?)
本文介绍了如何从 WinForms 应用程序中用户控件上的子控件中获取事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主窗体有什么方法可以拦截用户控件的子控件上触发的事件吗?

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.

现在,我在作弊.我有一个委托连接到子控件的焦点离开事件.这个委托改变了我没有在其他地方使用的用户控件的一个属性(在这个原因中,CausesValidation).然后,当用户控件的 CausesValidation 属性发生更改时,我在主窗体上定义了一个委托,然后指示应用更新数据并显示.

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.

推荐答案

最佳实践是在 UserControl 上公开事件,将事件冒泡到父窗体.我已经开始为你整理了一个例子.以下是此示例提供的内容的说明.

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
  • 使用 TextBox1
  • 创建一个 UserControl
  • UserControl 上注册一个名为 ControlChanged
  • 的公共事件
  • UserControl 中为 TextBox1 TextChangedEvent
  • 注册一个事件处理程序
  • TextChangeEvent 处理函数中,我调用 ControlChanged 事件以冒泡到父窗体
  • Form1
  • 在设计器上放置 UserControl1 的实例
  • UserControl1 上为 MouseLeaveControlChanged
  • 注册一个事件处理程序
  • UserControl1
  • Create a UserControl with TextBox1
  • Register a public event on the UserControl called ControlChanged
  • Within the UserControl register an event handler for the TextBox1 TextChangedEvent
  • Within the TextChangeEvent handler function I call the ControlChanged event to bubble to the parent form
  • Form1
  • Drop an instance of UserControl1 on the designer
  • Register an event handler on UserControl1 for MouseLeave and for ControlChanged

这是一个屏幕截图,说明我在 UserControl 上定义的 ControlChanged 事件可通过父 Windows 窗体上的 Visual Studio 中的 UX 获得.

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 应用程序中用户控件上的子控件中获取事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)