Adding the DicePanel Class to the Toolbox
The runtime addition of controls to a form is one way to get things done; however, as promised earlier in the chapter, you can add the DicePanel class to the Visual Studio Toolbox.You can customize the Visual Studio Toolbox by right-clicking on it. You can choose to add a new tab to the Toolbox to differentiate your controls from the default .NET controls. Selecting Add Tab from the menu accomplishes this and brings up an editor so you can type in the name of your new tab.Once you add the new tab, right-click again and select Add/Remove Items from the menu. The Customize Toolbox dialog box, shown in Figure 2-7, will display (make sure to be patient because this dialog box can take quite a while to load).
Figure 2-7: The Customize Toolbox dialog box, one of the slowest-loading dialog boxes in Visual Studio
Click the Browse button to add your new class library to the list of available class library references (first click the .NET Components tab at the top of the dialog box). Then, navigate to the DicePanel.dll file in the bin directory of the DicePanel project folder. Once added, you can select OK and the DicePanel will appear in the Toolbox! Now you can simply drag it from the Toolbox onto any form of any project. Doing so automatically creates a reference in that project to DicePanel.dll, similar to the way you created the reference manually in the previous example. Figure 2-8 shows the Properties window in design mode including all of the “for free” properties of the Panel class and the new properties of the DicePanel class, specifically NumDice, DebugDrawMode, and Result (shown in gray because it’s a read-only property).

Figure 2-8: The Properties window for an instance of the DicePanel class, showing the new properties. Try that in VB 6!
Tip | You can choose to add the DicePanel project to a blank solution as in the previous section if you plan to debug the DicePanel code. You don’t have to add the source code projects of your Toolbox classes just to use the class on a form, just like you don’t have to add a class library project containing the Button class just to use the Button class on a form. |
Having the new class properties display in the Properties window is really cool, but Figure 2-8 demonstrates a few minor problems. First, notice that all three of the new properties appear under the heading Misc. It would be nice if you could customize that heading for each property. Also, the little help box at the bottom of the Properties window doesn’t show any help, except for the name of the property in bold. It would be useful to add help for the programmers using your class.You can accomplish both of these tasks by using something called attributes. An attribute is a special type of class that can document, describe, or categorize your code. This is a new concept to pre-.NET programmers, so it requires some clarification. An attribute is an instance of a class that’s created and “attached” to various elements of your project; you can associate an attribute with a class or any member of a class such as a property, event, or method. These attributes can help document the author of the code and the last time it was modified, and they can define security constraints. One example of the usefulness of attributes is the ability to write a program that takes your compiled code as input, extracts the documentation-specific attributes, and outputs them into a specific format (Extensible Markup Language, Word document, and so on).Several built-in attributes help document class properties. Listing 2-25 shows the NumDice property on the DicePanel with three attributes attached to it.Listing 2.25: NumDice Property with Attributes
<Description("Number of Dice in the Panel"), _
Category("Dice"), _
DefaultValue(2)> _
Property NumDice() As Integer
Get
Return FNumDice
End Get
<... code removed ...>
End Property
Caution | You’ll need to add System.ComponentModel to the Imports section at the top of any code file that you want to extend through attributes. |
You define the attributes between brackets before the property name. Note the line-continuation characters, meaning that the attributes are actually on the same line as the member name (something I hope Microsoft changes in future versions of VB .NET, but oh well…).There are three attributes here: The Description attribute defines the little help text at the bottom of the Properties window, the Category attribute defines the name of the branch under which the attribute will sit in the Properties window, and the DefaultValue attribute defines, of all things, the default value of the property. After setting up all three of the DicePanel properties, the result is an improved Properties window, as shown in Figure 2-9.

Figure 2-9: The Properties window, showing how you now have complete mastery of creating properties, putting them in categories, and writing help, all by using attributes
As mentioned, attributes are a powerful concept. Consult the online help for more information. Use attributes and metadata as your search criteria to get you started.