.NET WinForms Custom Control: how to get a reference to the containing form
Is there a way, when creating a WinForms Custom Control/User Control, to get a reference at runtime to the form the control is placed on, so as to access various properties of that form? I would rather not pass a reference to the form into the constructor of the control, as, for various reasons, I need to keep a default constructor with no parameters.
One example: I have several Custom Controls that encapsulate Win32 API calls, and some of the calls require Window handles to be passed as parameters. I would like to be able to retrieve the handle of the containing form so I can pass it to the API function.
Another example: I have a Custom Control that provides "toast"-style user notifications. I would like to have the option of opening the notification form in a location relative to the location of the main application form, such as centered on the main window, off to the right, etc. This is not possible, obviously, without knowing the coordinates of the main application's window.
I could resort to using FindWindowEx()-type API calls in some cases, but that feels like a开发者_如何学C kludge, and would not work in all cases.
Does anyone know if this is possible?
You can use Control.TopLevelControl
property.
You want Control.Parent
, which returns the parent control. To get the form, simply call parent on all your parent controls until you hit a System.Windows.Forms.Form
.
Try the FindForm method. Be aware that it will return null
if called in your control's constructor.
If you can wait until the OnParentChanged
event, FindForm
will return a reference to the parent form then.
精彩评论