以主窗体捕获自定义控件事件 [英] Catch custom control event in main form

查看:57
本文介绍了以主窗体捕获自定义控件事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自定义控件,其中有一个面板。在这个面板内有另一个面板。这个内部面板有一个按钮。



现在我必须在第一个自定义控件中捕获按钮单击事件。怎么做?



我没有在第一个面板或自定义控件中创建按钮对象。我只在第二个面板中创建按钮对象,我将此按钮对象添加到第二个面板

I have custom control which has a panel in it. Inside this panel there is an another panel. This inner panel has a button.

Now I have to catch button click event in the first custom control. How to do it?

I am not creating button object in first panel or custom control. I am creating button object only in the second panel and I am adding this button object to this second panel

推荐答案

问题的表述不是很清楚,但是,基于许多类似的问题,这是在定制或用户控制的孩子的封装和处理事件发送给这些孩子的需要之间的争议,而不暴露孩子自己。



我可以建议处理(而非捕获,处理)内部公开所需的事件,并将它们委托给可从外部访问的其他事件。它需要一些额外的工作,但这是在复合用户或自定义控件的 fasade 下封装内部控件的整个想法。这是一个简单的想法:

The problem formulation is not quite clear, but, based on many similar problems, this is a controversy between encapsulation of children of the custom or user control and the need for handling events dispatch to those children, without exposing the children themselves.

I could advice to handle (not "catch", handle) the events you need to expose internally and delegate them to other events accessible from outside. It would need a bit too much of extra work, but this is the whole idea of encapsulation of inner controls under the fasade of a composite user or custom control. This is a simple illustration of the idea:
class MyUserControl : Control { // for example

    public MyUserControl() {
        // ...
        child.Click += (sender, eventArgs) => {
            if (ChildClick != null)
                ChildClick.Invoke(this, new System.EventArgs());
                // or, for example
                // ChildClick.Invoke(child, new System.EventArgs());
        } //permanent child.Click handler
    } //constructor

    // private, you don't expose it to the outside user:
    Button child = new Button(); 

    // ...

    public event System.EventHandler ChildClick;    

}



你明白了吗?具有更专业的事件参数类型的事件(从 System.EventArgs 派生,您自己的类型或在.NET FCL中预定义)也是如此。您使用


Are you getting the idea? Same things go with the events which have more specialized event arguments types (derived from System.EventArgs, your own type or predefined in .NET FCL). The you use

public event System.EventHandler<concrete_event_args_type> SomeExternalEvent;



并透明地将您在事件参数类型中携带的所有数据传递给事件调用( Invoke )对于子控件的事件。



如果您的自定义或用户控件中有多个层次结构,则执行相同操作,透明地传递来自内部控件事件的处理程序到其父控件。只需在多个级别上完成相同的工作。



-SA


and transparently pass all data you carry in your event arguments type to the event invocation (second parameter of Invoke) for the child control's event.

If you have more than two levels of hierarchy in your custom or user control, you do the same, transparently passing events from the handler of inner control's event to its parent control. Just do the same work on more than one level.

—SA


你通常会这样做处理UserControl中的按钮事件,然后从主表单订阅的UserControl中抛出一个新事件。



应该处理UserControl中控件的所有事件通过UserControl代码本身,而不是它之外。如果需要公开来自这些控件的事件,请在UserControl中创建自己的事件,并根据需要在UserControl代码中引发它们。然后,使用UserControl的表单将订阅这些新事件,而不是订阅控件事件。
You would normally handle the button event in your UserControl and then throw a new event from your UserControl that the main form subscribes to.

All the events for controls inside the UserControl should be handled by the UserControl code itself, not outside of it. If you need to expose events from these controls create your own events in your UserControl and raise them in the UserControl code as required. The form that uses the UserControl would then subscribe to these new events, not to the control events themselves.


这篇关于以主窗体捕获自定义控件事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆