Special Offers

Microsoft® Visual Basic® .NET Step by Step
Author Michael Halvorson
Pages 656
Disk 1 Companion CD(s)
Level Beg/Int
Published 01/23/2002
ISBN 9780735613744
ISBN-10 0-7356-1374-5
Price(USD) $39.99
To see this book's discounted price, select a reseller below.

More Information

About the Book
Table of Contents
Sample Chapter
Related Series
Related Books
About the Author

Support: Book & CD

Rate this book
Barnes Noble Amazon Quantum Books


Chapter 3: Working with Toolbox Controls continued

Using the DateTimePicker Control

Some Visual Basic controls display information, and others gather information from the user or process data behind the scenes. In this exercise, you'll work with the DateTimePicker control, which prompts the user for a date or time using a graphical calendar with scroll arrows. Although your use of the control will be rudimentary at this point, experimenting with DateTimePicker will give you an idea of how much Visual Basic controls can do for you automatically and how you process the information that comes from them.

The Birthday Program

The Birthday program uses a DateTimePicker control and a Button control to prompt the user for the date of his or her birthday and displays that information using a message box along with other information. Give it a try now.

Build the Birthday program

  1. On the File menu, click Close Solution to close the MyHello project.
  2. The files associated with the Hello World program are closed.

  3. On the File menu, point to New, and then click Project.
  4. The New Project dialog box appears.

  5. Create a new Visual Basic Windows Application project named MyBirthday in the c:\vbnetsbs\chap03 folder.
  6. The new project is created and a blank form appears in the Windows Forms Designer.

  7. Click the DateTimePicker control in the Toolbox.
  8. Draw a date time picker object in the middle of the form, as shown in the following:
  9. Click to view graphic
    Click to view graphic

    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 guide—it lets you size the date time picker object correctly when you are creating it.

  10. Click the Button control in the Toolbox, and then add a button object below the date time picker.
  11. You'll use this button to display your birth date and verify that the date time picker works correctly.

  12. In the Properties window, change the Text property of the button object to Show My Birthday.
  13. 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.

  14. Double-click the button object on the form, and then type the following program statement between the Private Sub and End Sub statements in the Button1_Click event procedure:
  15. MsgBox("Your birth date was " & DateTimePicker1.Text)
    MsgBox("Day of the year: " & _ 
    MsgBox("Today is " & DateTimePicker1.Value.Now.ToString())

    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.

    Click to view graphic
    Click to view graphic

  16. Click the Save All button to save your changes to disk.

Now you're ready to run the Birthday program.

Run the Birthday program

  1. Click the Start button on the Standard toolbar.
  2. The Birthday program starts to run in the development environment. The current date is displayed in the date time picker.

  3. Click the drop-down arrow in the date time picker to display the object in calendar view.
  4. Your form will look like the illustration on the following page. (You'll see a different date.)

    Click to view graphic
    Click to view graphic

  5. Click the left scroll arrow to look at previous months on the calendar.
  6. 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.

  7. Select the four-digit year in the date time picker text box.
  8. When you select the date, the date time picker will close.

  9. Type your birth year in place of the year that is currently selected, and then click the drop-down arrow again.
  10. The calendar reappears in the year of your birth.

  11. Click the scroll arrows again to locate the month in which you were born, and then click the exact day on which you were born.
  12. 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.

  13. Click the Show My Birthday button.
  14. 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:

    Click to view graphic
    Click to view graphic

  15. Click OK in the message box.
  16. A second message box appears indicating which day of the year you were born on.

  17. Click OK to display the final message box.
  18. The current date and time are displayed—the program works!

    You'll find the date time picker object to be quite capable—not 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.

  19. Click OK to close the message box, and then click the Close button on the form.
  20. You're finished using the DateTimePicker control for now.

Previous   |  Table of Contents   |   Next

Last Updated: January 12, 2002
Top of Page