Overriding a Procedure in a Parent from a child control
-
27-09-2019 - |
Question
I'm developing this usercontrol which requires I override the WndProc
of the control's parent [which for all practical purposes is a Windows Form] and I'm stumped.
Ordinarily, I could drop the user on the form and manually override the forms WndProc
.
Since my entire development team and possibly others I don't know of right now could be using it, I think it would be better I override from the UserControl.
Does anyone know how I can get this done? My preferred languages are VB.NET and C#.
Solution
You might try NativeWindow, getting a reference to the parent form via this.FindForm()
.
That is,
public class MyListener : NativeWindow
{
public MyListener(UserControl myControl)
{
this.AssignHandle(myControl.FindForm().Handle);
}
protected override void WndProc(ref Message m)
{
// do stuff
base.WndProc(ref m);
}
// dispose, etc.
}
Inheriting from NativeWindow
in this way would let you intercept the Windows messages.
Good luck!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow