将鼠标事件传递给Base Control,搜索一个好的解决方案

时间:2011-02-02 09:19:52

标签: c# events user-controls

喂,

感谢您的阅读。答案我得到了真正的帮助,很多!

但问题。我有UserControl作为基类。子类在UserControl上添加了它的控件,让我们先调用BaseControl和第二个ChildControl。

在BaseControl Level,我在一些MouseEvents上附加了一些代理。

问题是,如果添加了Child控件,它们将不会触发。所以我知道我可以从所有的儿童事件中取出所有的鼠标事件并将它们穿过。但那首先非常脏,一秒钟还不够。因为我想要画一个边框或移动BaseControl。

所以我的问题很重要:如果添加了ChildControls,有没有办法在Base Control中获得对MouseEvents的完全访问权限?

非常感谢!

托马斯

编辑:

以下是片段,希望您理解:

 public partial class baseControl : UserControl
{
    public baseControl()
    {

        InitializeComponent();

        //will not be called 
        this.MouseUp += delegate(object sender, MouseEventArgs e)
        {
            // some code
        };
    }

}

public partial class child : baseControl
{
    secretControl childControl; 
    public child()
    {
        InitializeComponent();
        this.childControl= new secretControl ();
        this.childControl.Visible = true;
        //... ,more 
        this.forChildUsePanel.Add(this.childControl);


        // works fine , as it will be called
        this.childControl.MouseUp += delegate(object sender, MouseEventArgs e)
        {
            // some code
        };


    }

}

提醒:这是snipptes,所以为了理解目的,它会削减一些角落。

Simpelput: 我希望在每个Control上添加一些鼠标事件添加到BaseControl。

3 个答案:

答案 0 :(得分:0)

我相信你应该将事件处理程序附加到每个子控件而不是父控件,如果你想在子控件中拦截它。

答案 1 :(得分:0)

您要添加哪种控件?它可能是自定义这些控件的解决方案。例如,如果添加Label,请创建一个新的MousePassthroughLabel,它继承自Label,其内部MouseEvent只是触发其父控件之一。对每种类型的控件执行一次此操作并使用您自己的“MousePassthrough” - 控件而不是本机控件。

答案 2 :(得分:0)

有一种新的Microsoft技术Reactive Extensions,它将使事件驱动编程更容易。