logo
Учебник_ПОА

To display the time by using a DateTimePicker control

  1. Add a DateTimePicker control to the form, with the default name DateTimePicker1.

  2. Double-click the form to switch to the Code Editor.

  3. Add the following code to the Form1_Load event handler. This code sets the format of the control to display a time, instead of a date, and lets the user change the time that is displayed.

    this.dateTimePicker1.Format = DateTimePickerFormat.Time;

    this.dateTimePicker1.Width = 100;

    this.dateTimePicker1.ShowUpDown = true;

  4. Add a Button control to the form, and change the following properties.

    Property

    Value

    Name

    currentTime

    Text

    Current Time

  5. Double-click the button to add the default Click event handler.

  6. Add the following code to set the time back to the current time.

    this.dateTimePicker1.Value = DateTime.Now;

  7. Press F5 to run the program.

  8. When the form opens, change the time by selecting the hour, minute, or seconds and click the up or down arrow.

  9. Click Current Time to reset the control to the current time.