In many of our projects we might want to create custom controls (that means customized textboxes/buttons/panels and many other). To make application designing easier, we can also add those custom controls in Designer View and in the ToolBox.

How to?
Let’s say we want to add Designer support for this custom button:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class MyCustomButton : Button { public MyCustomButton() { this.BackColor = Color.Black; this.ForeColor = Color.DodgerBlue; this.Size = new Size(200, 200); this.Font = new Font("Verdana", 14, FontStyle.Regular); } } |
In order to do this you have to follow those 2 steps:
1) Include this namespace:
|
1 2 |
using System.ComponentModel; |
2) Add right before your class header this line:
|
1 2 |
[ToolboxItem(true)] |
This will add the custom button in the ToolBox (where the default controls are shown) – all you have to do now is simply drag and drop the control in your form and it will be rendered acording to it’s class (which is MyCustomButton).
The code should look like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[ToolboxItem(true)] //this is the line I added public class MyCustomButton : Button { public MyCustomButton() { this.BackColor = Color.Black; this.ForeColor = Color.DodgerBlue; this.Size = new Size(200, 200); this.Font = new Font("Verdana", 14, FontStyle.Regular); } } |
