How to generate custom code in the "Windows Form Designer generated code" section

G

Guest

I'm developing a composite winforms control that derives from System.Windows.Forms.UserControl, and I want it to behave as a singleton so that each form will access the same instance. When I implement the singleton design pattern by making the constructor private, the VS IDE throws an error when I drag the control on to a form
"An error occurred while trying to create an instance of 'NavigationControl'. The exception was "Constructor on type 'NavigationControl' not found.

To solve the problem, I need the IDE to create custom code when my control is dragged on to a form. Instead of "NavigationControl1 = New NavigationControl", I need NavigationControl1 = NavigationControl.CreateInstance

Does anyone know how to override the System.Windows.Forms.UserControl's designer to accomplish this

Thank you!
 
P

Palo Mraz

To solve the problem, I need the IDE to create custom code when my control
is dragged on to a form. Instead of "NavigationControl1 = New
NavigationControl", I need NavigationControl1 =
NavigationControl.CreateInstance.

You'll have to implement a TypeConverter for your control that performs
conversion to an InstanceDescriptor. In the ConvertTo method, initialize the
InstanceDescriptor to reference your Shared creation method. Shawn Burke has
a good explanation - see
http://msdn.microsoft.com/library/en-us/dndotnet/html/custcodegen.asp?frame=true

Palo
--
http://dact.lamarvin.com/
AutoComplete component for WinForms applications. Easy to integrate, easier
to use!
http://www.vbinfozine.com/
An ordinary VB developer shares his own successes and failures
 
P

Palo Mraz

Your suggestion worked perfectly, but there's one more issue. Since I'm
implementing the Singleton design pattern, my control's constructor is
scoped as "Private". When I drag my control on to a form at design time, I
get the following error:
"Constructor on type 'my control type' not found."

Do you know of any way to prevent the IDE from checking for a public
contructor?

I'm sorry but I don't. Maybe you could use the ToolboxItemAttribute and
create your own ToolboxItem descendant, but the documentation for these
things is sparse and I've never tried it. I'd suspect that that even if you
solve the creation problem, you might run into other issues concerning
siting and parenting the singleton control... (I've never seen a singleton
control implementation, so I might be wrong:)

Palo
---
http://dact.lamarvin.com/
AutoComplete component for WinForms applications.
Easy to integrate, easier to use!
http://www.vbinfozine.com/
An ordinary VB developer shares his own successes
and failures.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top