How to create a Custom Form that appears in the Toolbox and is droppable on the Design Surfa

I like to create a dll with a custom form that contains other controls (buttons, check boxes, etc). I am using vb.net or c#.net (2005). I like the form to appear in the Toolbox and be able to drop it on the design surface. This behavior is the same as the provided dialogs such as FontDialog.

Searching the web showed that I need to add a designer attribute of ComponentDesigner: "Set the Designer attribute for your custom dialog to be ComponentDesigner (instead of the default ControlDesigner). That should make it droppable in the design surface as a component."

But I have not been able to make this work. Can someone help? Thanks,

Rene

[676 byte] By [RMan54] at [2007-12-22]
# 2

The link does not answer my question. My usercontrols are not disappearing.

My issue is to use a form or dialog that contains several other controls and use it as a Component so that it can be displayed in the toolbox and be placed on the design surface of another form during design time (FontDialog and ColorDialog fro example work this way). I think that this works only with Components. Do I need to implement the IComponent and IDisposible interface myself in the form? If so, how do I do this?

Thanks,

-Rene

RMan54 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 3
have you tried to look at the codes of the dialog components using Reflector perhaps? Basically you're component must derive from Component or implement IComponent and not from Control/Form for it to appear just in the tray.
joeycalisay at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 4

Hello Rene,

If you inherit from ComponentDesigner instead of ControlDesigner I think you will be going in the right direction.

A Designer class is a special class that helps you with Design time tasks. It is connected with your regular class via an attribute. As an example, let's say you had a class named MyClass and a corresponding designer class which inherited from ComponentDesigner named MyClassDesigner.

<Designer(GetType(MyClassDesigner), GetType(IDesigner))> _
Public Class MyClass

You may also want to check MSDN for ControlDesigner Class and ComponentDesigner Class.

Good Luck!

j2associates at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 5
Does your control appear in the Toolbox? If not use the Add Items... and browse for your *.dll and select it. This should place the controls in the ToolBox.
JRQ at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 6

Thanks for all the comments. I tried different things but my conclusion is that a dialog needs to inherit from the component class in able to be placed in the toolbox. No designer attributes applied to a class that inherits from form can change this.

A dll needs to contain classes that inherit from usercontrol (for controls) or component (dialogs inherited from CommonDialog for example or custom functional classes without visual elements) in order to be placed in the toolbox. If a dll doesn't contain any of these, you get the following error message in vs when trying to import in the toolbox: "There are no components in ....dll that can be placed on the toolbox".

I see two options to solve my problem:

1. Have my class inherit from component and program the key "form" functionality myself.

2. Have a class that inherits from component and that encapsulates my form. I tried this option and it works fine (component can be added to the toolbox and placed on the design surface). The drawback is that I need to add properties, methods and event handlers in the component class that manages the form and that are displayed in the properties window at design time.

I am interested in opinions on how to do this better if possible.

Thanks,

Rene

RMan54 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 7

Rene,

Are you planning on the users of your designer modifying the form you have in your dll by adding additional controls, or just setting properties and handling events for the form?

Ken_Bussell at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 8

Just setting properties and handling events.

Rene

RMan54 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 9

Do the users actually need to see the form on the design surface, or can they just have a place holder somewhere that they can use to get at its properties?

I'm asking the questions because it will help get a better idea of what you are trying to do.

Ken_Bussell at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 10

Ok I re-read your post and I think I have an answer for you.

Build yourself an object derived from System.ComponentModel.Component.

Add a ToolBoxItem to that class. (see the help on ToolboxItem Class for a some good example code on how to do this.)

Lastly you will need to add a custom designer to your component so that when the user double clicks (invoking DoDefaultAction) you can show your form to manipulate your properties.

Did this help?

Ken_Bussell at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 11

I want to repeat the functionality of for example the FontDialog or ColorDialog.

None of the toolbox items that are placed on the "design surface" are fully visible by the programmer. Controls are placed on a form at design time. Components are different and when they are dragged to a form at design time, an instance is added to the form and shown by the icon and name (Fontdialog1, for example) on the design surface. The "design surface" is the area beneath the form in the designer. When selected by the programmer, properties can be set in the properties window. Another example, is the Timer that can be dragged from the Toolbox to the Design Surface. The difference with the Timer is that there are no visisble elements, while the dialogs inherited from CommonDialog such as FontDialog and ColorDialog have visible elements but their are only visible when shown at runtime in the program code. That's what I am trying to duplicate for a custom dialog (or form), inlcuding having the dialog appear in the Toolbox and having it shown on the design surface so that the programmer can set the properties.

Hope this helps as an explanation.

-Rene

RMan54 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 12
Let me know if this was helpful at all.
Ken_Bussell at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 13
You can also also design a UI-less component and have the your Form as a private object. Expose some properties and method to set some values for your for and to show your form.
JRQ at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 14

This is Method #1 described in one of my previous posts on this topic. That works fine although there is some redundancy in having to channel the form properties and methods to the component. In addition, events originating in the form need to be handled by the component as an intermediate.

RMan54 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...