Can someone point me in the direction of a good tutorial on creating and building custom controls that can be added to the Toolbox?
Thanks in advance for the help.
Printable View
Can someone point me in the direction of a good tutorial on creating and building custom controls that can be added to the Toolbox?
Thanks in advance for the help.
There is no general tutorial. It all depends on what you want your control to do.
In general though, there are two types:
- UserControls.
- Custom controls.
UserControls are a type of 'container' you can add controls to, which then shows up in the toolbox as a single control. Think of a TextBox with a ToolStrip above it, or something like that.
Custom controls are simply classes that inherit from some other control and extend their behavior. To inherit from some control just use the Inherits statement. For example, to inherit a Button you can do
Now, the class CustomButton will behave exactly like an ordinary button (even though it's only 3 lines of code!). You can now start extending it by adding methods or properties, or by overriding existing properties (using the Overrides statement; just type 'Overrides' and use the Intellisense to see what you can do).Code:Public Class CustomButton
Inherits Button
End Class
EDIT
I should have added... In order for a class to appear in the Toolbox, all that is required is that it implements IComponent, either directly or indirectly. Since both the UserControl and the Control class (from which, in turn, all other controls such as buttons inherit) implement IComponent, they will all appear in your toolbox. The only thing you need to do is Build your solution successfully.
Nick, thanks, but I'm really just looking for a tutorial on the process to go through to build it and use it from the toolbox. I'm familiar with extending properties of a control within a user created class in my project. Just don't know how to go from that to being able to add it to the toolbox so that I can drop it into a projects form from there and re-use it in other projects.
If you are already familiar with extending classes, then all you need to do is extend an existing Control (or inherit Control directly, or implement IComponent directly) and then build your solution. To build, just go to the Build menu and click Build :) Then, if the build succeeded, the control should show up at the top of the toolbox automatically (as long as you are in the same project of course).
Thanks Nick.
Ok, now I'm really feeling dumb. I've always set up a custom control by creating a class and put them on the form using code from the form.
After trying to resolve this I finally figured out that somewhere along the line I had Tools>Options Windows Forms Designer AutoToolboxPopulate set to False!
Very frustrating. Thanks again for your help.
I'm glad you resolved that issue by yourself because I've never seen that option before. Good to know that it's there though :)