views:

34

answers:

1

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#.

+1  A: 

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!

Nicholas Piasecki
Thanks for the Info. I'll try it out and see what happens.
Alex Essilfie