Sign in to follow this  
devronious

[.net] Adding custom components to toolbox...

Recommended Posts

Anybody know how to add a new component to the toolbox? I created a Control which inherits from a Component and try to compile into dll then add to toolbox custom tab. It works if I create a Component but not if it's a Control. What an I doing wrong? I would like to make custom Controls that I can simply drag into my forms. Thanks in advance, Devin

Share this post


Link to post
Share on other sites
What do you mean "inherits from a Component"? If it's a Control Library project, you just right-click on the toolbox, select "Choose Items..." and select the DLL.

Share this post


Link to post
Share on other sites
Right, thanks, got it. OK now I can add my custom controls and make them more elaborate for use in each project. The question I have now is what if I want to start with something other than a Control?

I noticed that the UserControl is the default derived class used when creating these projects. I wanted to make a custom Control that inherited from the ComboBox. But when I put the ComboBox in place of the UserControl in the code file something strange happened in the graphic designer:

I just got a component display which didn't show any graphics at all. I was hoping to see a preview of my custom ComboBox, not component icons. Is there a way to inherit from already established controls and still visually design them in Visual Studio?

Does this make sense what I'm asking? Or do I need to re-phrase? I'm not that great with sentance structure.

-Devin

Share this post


Link to post
Share on other sites
Of course, you can derive from any descendant of Control and still get design time support. You will, of course, inherit any design time support provided by the base class, so that may screw up what you are trying to do.

Share this post


Link to post
Share on other sites
Oh, I see what you are saying. You are talking about when you double click on a custom control file in Visual Studio. That will bring up the component designer, which is really only useful when you derive from items like Panel. For combo box, the component designer is useless.

Simply write out the details of your class by right clicking on the file and then selecting View Code, and then when you add your custom control to a Form, it will appear to be a combo box whose properties (including any custom ones you added) can be edited in the property grid.

Share this post


Link to post
Share on other sites
Mike,

Cool you see what I mean. Thanks for taking the time and checking that, I'm a bit frustrated with trying to figure it out.

Quote:
Simply write out the details of your class by right clicking on the file and then selecting View Code, and then when you add your custom control to a Form, it will appear to be a combo box whose properties (including any custom ones you added) can be edited in the property grid.


I'm not sure what you mean? And the sad thing is that I'm thinking it's probably really obvious? Am I right? Can you re-phrase that please?

-Devin

Share this post


Link to post
Share on other sites
OK, here's the deal. Whenever a source file in the solution explorer contains a class (the first class in the file I think) that inherits from Component or Control or and of their derivatives, Visual Studio notices and marks that file as a custom control. It also changes the icon from then normal green C# source file into one that's supposed to represent a custom control.

Now, once this happens, double clicking on the file in the solution explorer will no longer bring up a code window. Instead, it will show a graphical designer for the control/component, which is usually just an empty square if the control can contain others on it (like a Panel), or an empty page with the words "To add components to your class, drag them from the Toolbox and use the Properties window to set their properties. To create events and methods for your class, click here to switch to code view."

This view isn't very useful for controls like a combo box, since combo boxes don't have other controls on top of them usually. So instead, switch to the code view and add any properties / events / methods that you want your custom control to have right in the source code.

Once you have finished writing your custom control, go to a FORM editor (ie. the editor that shows up when you double click a form in the solution explorer). Now you can add your custom control to the form by dragging it from the toolbox (it usually has a purple gear icon unless you specify a different one). Once the control is on the form, you can edit its properties and hook up events just like any other .NET control.

Share this post


Link to post
Share on other sites
Cool, that's what I needed. Just one other thing. And you just about mentioned it. What about the icon for the toolbox? How can I change it from the purple gear to something else?

-Devin

Share this post


Link to post
Share on other sites
IIRC, you apply a ToolBoxBitmapAttribute to your class


[ToolBoxBitmap("Somefile.bmp")]
class MyClass : Control
{}

Share this post


Link to post
Share on other sites
You can do two things. First, you can simply add a bitmap file to your project with the same name as your control (ie. MyControl.bmp). It should be 16x16 and use only 16 colors. Once added, change the build action for the file to Embedded Resource. Once the project is recompiled, the bitmap should automagically be used.

If you need more control, you can make use of the ToolboxBitmap attribute. Using this attribute you can specify a different file name, or even steal the bitmap from another control.

For example:
[ToolboxBitmap(typeof(MyControl), "ControlImage.bmp")]
- Will attach the bitmap in ControlImage.bmp to the control.

[ToolboxBitmap(typeof(TreeView)]
- Will attach the bitmap associated with the .NET TreeView control with your control as well.

Share this post


Link to post
Share on other sites
Super! Thanks so much for the help. I'm totally thrilled now. For about 4-5 years I've been programming but just now have I found that I can create my own Control groups for use in the tool box of the Designer. Works super and really is opening up my world.

-Devin

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this