Chapter 16 Inheriting Forms and Creating Base Classes 401
Adding a New Class to Your Project
Simply stated, a class in Visual Basic is a representation or blueprint that defines the
structure of one or more objects. Creating a class allows you to define your own objects in
a program—objects that have properties, methods, fields, and events, just like the objects
that the Toolbox controls create on Windows forms. To add a new class to your project, you
click the Add Class command on the Project menu, and then you define the class by using
program code and a few Visual Basic keywords.
In the following exercise, you’ll create a program that prompts a new employee for his or
her first name, last name, and date of birth. You’ll store this information in the properties of
a new class named Person, and you’ll create a method in the class to compute the current
age of the new employee. This project will teach you how to create your own classes and also
how to use the classes in the event procedures of your program.
Build the Person Class project
- Click the Close Project command on the File menu, and then create a new Windows
Forms Application project named My Person Class. - Use the Label control to add a label object to the top of Form1.
- Use the TextBox control to draw two wide text box objects below the label object.
- Use the DateTimePicker control to draw a date time picker object below the text box
objects.
You last used the DateTimePicker control to enter dates in Chapter 3, “Working with
Toolbox Controls .” Go to that chapter if you want to review this control’s basic methods
and properties. - Use the Button control to draw a button object below the date/time picker object.
- Set the following properties for the objects on the form:
Object Property Setting
Label1 Text “Enter employee first name, last name, and date of birth .”
TextBox1 Text “First name”
TextBox2 Text “Last name”
Button1 Text “Display record”
Form1 Text “Person Class”