Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this lesson, you will learn how to create your own control using the User Control Designer.
In the previous set of lessons, you learned how to use a Class Library project to create a class. A user control is simply a class that you can see. Just like the standard controls that come with Visual Basic, a user control can be placed on a form at design time, and it appears when you run the program.
When you are designing your program, the form designer is where you arrange your controls and decide how they look. There is also a designer for user controls, the User Control Designer, that lets you, the developer, decide what the control will look like.
A User Control is similar to any other class, but with the added ability to be placed in the Toolbox and displayed on a form. Where a Class module has only code, a user control module has both code and a designer. The User Control Designer is similar to a form designer—it has properties for controlling the appearance and behavior of the user control.
The ways to create a user control differ slightly, depending on the version of Visual Basic that you are using: Visual Basic 2008 has a Windows Forms Control Library project type; in Visual Basic Express, you must first create a Class Library project and then add a User Control template.
On the File menu, click New Project.
On the Templates pane, in the New Project dialog box, click Class Library and then click OK.
On the Project menu, click Add User Control.
In the Add New Item dialog box, select User Control.
In the Name box, type NamesControl and then click Add.
A new User Control template is added to the project and the User Control Designer opens.
In the Solution Explorer, right-click Class1.vb and choose Delete, and then click OK.
On the File menu, click Save All.
In the Save Project dialog box, type NamesUserControl and then click Save.
On the File menu, click New Project.
On the Templates pane, in the New Project dialog box, click Windows Forms Control Library.
In the Name box, type NamesControl and then click OK.
A new User Control template is added to the project, and the User Control Designer opens.
On the File menu, click Save All.
In the Save Project dialog box, type NamesUserControl and then click Save.
In this lesson, you learned how to create a project that has a user control and how to display the User Control Designer. A blank control is of little use though—in the next lesson, you will learn how to add controls to your user control to create its user interface.
Next Lesson: Adding Controls to Your User Control.
Visible Objects: Creating Your First User Control