Chapter 3: Working with Toolbox Controls continued
Build the Birthday program
The files associated with the Hello World program are closed.
The New Project dialog box appears.
The new project is created and a blank form appears in the Windows Forms Designer.
The date time picker object by default displays the current date, but you can adjust the date displayed by changing the object's Value property. Displaying the date is a handy design guideit lets you size the date time picker object correctly when you are creating it.
You'll use this button to display your birth date and verify that the date time picker works correctly.
Now you'll add a few lines of program code to a procedure associated with the button object. This procedure is called an event procedure because it runs when an event, such as a mouse click, occurs in the object.
MsgBox("Your birth date was " & DateTimePicker1.Text)
These program statements display three successive message boxes (small dialog boxes) with information from the date time picker object. The first line uses the Text property of the date time picker to display the birth date information you select when using the object at runtime. The MsgBox function displays the string value "Your birth date was" in addition to the textual value held in the date time picker's Text property. These two pieces of information are joined together by the string concatenation operator (&). You'll learn more about the MsgBox function and the string concatenation operator in Chapter 5.
The second and third lines collectively form one program statement and have been broken by the line continuation character (_) because the statement was a bit too long to print in our book. (See Tip box below for an explanation of this useful convention for breaking longer lines.) The statement DateTimePicker1.Value.DayOfYear.ToString() uses the date time picker object to calculate the day of the year in which you were born, counting from January 1. This is accomplished by the DayOfYear property and the ToString method, which converts the numeric result of the date calculation to a textual value that is more easily displayed by the MsgBox function.
Methods are special statements that perform an action or a service for a particular object, such as converting a number to a string or adding items to a list box. Methods differ from properties, which contain a value, and event procedures, which execute when a user manipulates an object. Methods can also be shared among objects, so when you learn how to use one method, you'll often be able to apply it to several circumstances. We'll discuss several important methods as you work through this book.
The fourth line in the program code uses the Now property to check your computer's system clock for the current date and time and displays that information in a message box after converting it to a string, or textual, value.
After you enter the code for the Button1_Click event procedure, the Code Editor should look similar to the following illustration.
Now you're ready to run the Birthday program.
Run the Birthday program
The Birthday program starts to run in the development environment. The current date is displayed in the date time picker.
Your form will look like the illustration on the following page. (You'll see a different date.)
Notice that the text box portion of the object also changes as you scroll the date. The "today" value at the bottom of the calendar doesn't change, however.
Although you could scroll all the way back to your exact birthday, you may not have the patience to scroll month by month. To move to your birth year faster, select the year value in the date time picker text box and enter a new date.
When you select the date, the date time picker will close.
The calendar reappears in the year of your birth.
If you didn't know the day of the week you were born on, now you can find out!
When you select the final date, the date time picker closes, and your birth date is displayed in the text box. Now click the button object to see how this information is made available to other objects on your form.
Visual Basic executes your program code and displays a message box containing the day and date of your birth. Notice how the two dates match:
A second message box appears indicating which day of the year you were born on.
The current date and time are displayedthe program works!
You'll find the date time picker object to be quite capablenot only does it remember the new date or time information that you enter, but it keeps track of the current date and time as well, and it can display this date and time information in a variety of useful formats.
You're finished using the DateTimePicker control for now.