Passing Mouse Events to Base Control, search of a good solution
Hallo,
Thank you for reading. The Answers i get realy helped me, A LOT!
But to the Problem. I have UserControl as a base Class. The child Class adds its Controls开发者_StackOverflow中文版 on the UserControl, lets call first BaseControl and the second ChildControl.
At BaseControl Level i have some Delegates attached on some MouseEvents.
The Problem is that they will not fire, if there is an Child control added to them. So I know i can Take all the Mouseevents from all the the Childrensevent and tunnle them through. But thats first very dirty an second not enough. Cause i want to like Paint a Border around or move the BaseControl.
So my Question is realy: Is there a way to gain full access to the MouseEvents in at Base Control, if there are ChildControls added?
Thank you lots!
Thomas
EDIT:
Here is the snippets, hope you understand:
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
};
}
}
Reminder: This are snipptes, so for understanding purpose it cut some corners.
Simpelput: I want to get some Mouse Event on every Control added to the BaseControl.
I believe you should attach the event handler to every single child control and not to the parent control, if you want to intercept it in the child.
What kind of controls are you adding? It might be a solution to customize these controls. If you add a Label for example, make a new MousePassthroughLabel which inherits from Label und whose inbuild MouseEvent simply fires the one of its parent control. Do this once for every type of control and use you own "MousePassthrough"-Controls instead of the native controls.
There is this new Microsoft technology, Reactive Extensions, which will make event-driven programming easier.
精彩评论