Understanding the User Control Designer

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.

To create a user control using Visual Basic Express Edition

  1. On the File menu, click New Project.

  2. On the Templates pane, in the New Project dialog box, click Class Library and then click OK.

  3. On the Project menu, click Add User Control.

  4. In the Add New Item dialog box, select User Control.

  5. 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.

  6. In the Solution Explorer, right-click Class1.vb and choose Delete, and then click OK.

  7. On the File menu, click Save All.

  8. In the Save Project dialog box, type NamesUserControl and then click Save.

To create a user control using Visual Studio

  1. On the File menu, click New Project.

  2. On the Templates pane, in the New Project dialog box, click Windows Forms Control Library.

  3. 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.

  4. On the File menu, click Save All.

  5. 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.

Show: