Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_for_Beginners.doc
Скачиваний:
28
Добавлен:
23.11.2019
Размер:
2.4 Mб
Скачать

Извлечение даты в элементе управления "DateTimePicker"

В этом примере происходит выбор даты встречи в элементе управления DateTimePicker Windows Forms. Выбранная дата встречи и текущая дата будут отображены в окнах сообщений.

Пример

-----

Компиляция кода

Для этого примера необходимы следующие компоненты.

  • Форма Windows Form с элементом управления DateTimePicker, которому задано имя dateTimePicker1, и элемент управления Button с надписью Выбор даты встречи.

  • Задайте обработчику событий кнопки значение button1_Click.

How to: Display the Time in a Label

This example displays the current time in a Label.

Example

private void displayTime()

{

Label1.Text = DateTime.Now.ToShortTimeString();

}

Compiling the Code

The form must contain a Labelnamed Label1.

Отображение времени в надписи

В этом примере текущее время отображается в Label.

Пример

private void displayTime()

{

Label1.Text = DateTime.Now.ToShortTimeString();

}

Компиляция кода

Форма должна содержать Labelс именем "Label1".9

How to: Display the Date and Time in Your Application

You can display the date on a Windows Form by using calendar controls, such as the MonthCalendar control or a DateTimePicker control. The DateTimePicker control also enables you to display the time.

You can also use these controls to collect input from the user to use the date or time selected elsewhere in your application. The MonthCalendar control enables you to select a range of dates.

To display a date by using a MonthCalendar control

  1. On the File menu, click New Project.

The New Project dialog box appears.

  1. Click Windows Forms Application and then click OK.

  2. Add a Label control to the form, with the default name label1.

  3. Add a MonthCalendar control to the form, with the default name MonthCalendar1.

  4. Double-click the form to add the default Load event handler in the Code Editor and add the following code. This code assigns the selected date (today's date) to the Text property of the label in short date format.

    this.label1.Text = this.monthCalendar1.SelectionRange.Start.ToShortDateString();

  5. Create a DateChanged event handler for the MonthCalendar1 control. You can do this by double-clicking the control in the designer.

  6. Add the following code to the MonthCalendar_DateChanged event handler. This code sets the label to the selected date, but this time in long date format.

    this.label1.Text = this.monthCalendar1.SelectionRange.Start.ToShortDateString();

  7. Press F5 to run the program.

  8. When the form opens, change the date by clicking a date in the MonthCalendar control.

  9. Verify that the date is updated in the label.

Отображение даты и времени в приложении

Отобразить дату и время в форме Windows Forms можно с помощью элементов управления "Calendar", таких как MonthCalendar или DateTimePicker. Элемент управления DateTimePicker позволяет отображать время.

Кроме того, эти элементы управления предназначены для получения от пользователя выбранной даты или времени и использования этих сведений в дальнейшем. Элемент управления MonthCalendar позволяет выбрать диапазон дат.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]