question

RobertGustafson-1682 avatar image
0 Votes"
RobertGustafson-1682 asked RobertGustafson-1682 edited

A Few Questions about using Subclassing when defining an "Extended" custom control

WHAT I HAVE:
Visual Basic 2019, .NET 4+, WinForms

MY PROBLEM:
I want to create a control that inherits from another and implements features not normally supported by the base class using subclassing. The subclassing would be implemented in the code defining the derived class. A few questions:

  1. When defining the window handle for SetWindowSubclass and other related procedures (I'm trying to capture messages for an auxiliary handle not directly exposed by the control itself), should I use Me.Handle (the handle of the derived class) or MyBase.Handle (the handle of the base class) for the hWnd parameter?

  2. Since there are likely to be multiple instances of the derived class, and they're all going to use the same "target procedure" to intercept messages, does the uIdSubclass parameter for SetWindowSubclass have to be different for each instance, or can it be the same? (If it has to be unique, how can I guarantee that?)

  3. Logically, I would invoke SetWindowSubclass after MyBase.OnHandleCreated from within the OnHandleCreated method. Should I also invoke RemoveWindowSubclass before MyBase.OnHandleDestroyed from within the OnHandleDestroyed method? (Particularly if the handle being subclassed is actually an auxiliary one, like the one for the drop-down list of a combo box?)


Please provide any advice/code ASAP, and in VB.NET.



windows-apidotnet-visual-basicwindows-forms
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RobertGustafson-1682,
The windows-forms tag is discussing and asking questions about the Windows Forms such as Winforms controls, libraries, samples, publication and installation.
You questions are mostly related to Win32 API, I suggest you modify tag for professional answer.
The supported products are listed over here (more to be added later on).
Thank you for your understanding.
Best Regards,
Daniel Zhang


0 Votes 0 ·

It seems that you want to inherit from another Forms control. Then SetWindowSubclass is not needed because you can handle the messages via WndProc (which can be overridden by your derived class) or existing events. I think that subclassing with SetWindowSubclass can be used for controls that are not available in Windows Forms.

0 Votes 0 ·

WndProc doesn't work if you're trying to subclass an auxiliary handle not exposed by the control, such as the handle of the drop-down list or edit portion of a combo box. In any case, please give me advise vis-a-vis SetWindowSubclass and RemoveWindowSubclass APIs. Even if you offer alternatives, please answer the 3 questions that I actually asked. (In particular, I'm trying to create a custom ComboBox control that supports mouse events for the drop-down list. I know how to do all this [theoretically]--provided I have a correct handle to the overall control [Me.Handle or MyBase.Handle? Is there a difference?], and I know whether or not I have to remove the subclass when the control's handle is destroyed and whether or not I need to guarantee a unique subclass ID for the inherited control.)











0 Votes 0 ·

0 Answers