使用自定义组件 Windows 窗体中的控件处理事件

发布于 2024-10-08 21:14:29 字数 311 浏览 0 评论 0原文

我有一个自定义组件,它基本上需要验证表单上的许多控件。这个想法是,程序员在设计时可以选择他们想要验证的控件列表。在运行时,我想处理每个选定控件的 Change 事件,并启动验证过程。

我已经创建了我的组件。它有一个 List 属性,用于存储程序员在设计时选择的控件列表(我实现了自己的 ListEditor 来实现此功能)。现在,在运行时,我想将列表中控件的 Changed 事件连接到 Validate 方法...这就是我陷入困境的地方。在运行时调试组件时,它永远不会进入构造函数。如果我在设计时添加事件处理程序,它们不会在运行时触发。

有什么想法吗?

谢谢

I have a custom component which basically needs to validate a number of controls on a form. The idea is that the programmer, at design time, can select a list of controls they would like to validate. At run-time, I would like to handle the Change event of each of the selected controls, and kick off the validation process.

I have created my component. It has a List property which stores the list of controls the programmer selected at design time (I implemented my own ListEditor to get this working). Now, at run time, I would like to wire up the Changed events of the controls in the list to the Validate method... And this is where I get stuck. when debugging the component at runtime, it never goes into the constructors. If I add the event handlers at design time, they don't fire at run-time.

any ideas?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2024-10-15 21:14:29

希望我的回答比原来的问题更具描述性。

基本上我的问题很简单。我有一个自定义组件,其主要职责是对组件所在表单上的一组控件进行一些验证。在设计时,用户从列表中选择这些组件。选定的控件存储/序列化在组件的属性(通用列表)中。在运行时,当列表中的控件之一发生更改(即触发 TextChanged)时,验证例程需要在所有选定的控件上运行。

我的问题是我需要在运行时向每个选定的控件添加一个事件处理程序。存储列表的属性的设置器仅在设计时调用(在用户选择他们想要验证的控件之后),这意味着此时添加处理程序将不起作用。

解决方案是什么?我没有使用通用列表,而是使用了 BindingList(不能使用 ObservableCollection....net 2.0),并处理了它的 ListChanged 事件。当在运行时将新控件添加到列表中时,我会连接该控件的事件。然后该事件被触发,一切都按预期进行。

Hopefully my answer is a little more descriptive than the original question.

Basically my problem is pretty simple. I have a custom component who's main duty is to do some validation on a group of controls on the form the component resides. At design time, the user selects these components from a list. The selected controls are stored / serialized in a property of the component (a generic List). At runtime, when one of the controls in the list are changed (i.e. TextChanged is fired), the validation routine needs to run on all the selected controls.

My problem was that I needed to add an event handler to each of the selected controls at run time. The setter for the property which stores the list is only called at design time (after the user selects the controls they want to validate), which means that adding the handler at this point is not going to work.

The solution? Instead of using a generic List I used a BindingList (can't use ObservableCollection... .net 2.0), and handled it's ListChanged event. When a new control is added to the List at run-time, I wire up the event for that control. The event is then fired, and everything works as it should.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文