Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

on a form using the DateTimePicker class, as summarized in .NET Table 11.3. This class displays a date and/or time to the user, and allows the user to change the values from the keyboard or from a dropdown calendar control. The dropdown calendar is based on the MonthCalendar class, which we will examine in the next section.

.NET Table 11.3 DateTimePicker class

The DateTimePicker class represents a date and/or time value on a form. It allows the user to select a specific date and/or time, and presents this selection in a specified format. The DateTime value is presented in a text box control, with a down arrow providing access to a calendar from which an alternate date can be selected. The various parts of the DateTime value can alternately be modified using an up-down button or the arrow keys on the keyboard.

This class is part of the System.Windows.Forms namespace, and inherits from the Control class. See .NET Table 4.1 on page 104 for a list of members inherited from this class.

 

CalendarFont

Gets or sets the Font to apply to the calendar

 

 

portion of the control.

 

CalendarForeColor

Gets or sets the foreground color for the

 

 

calendar.

 

Checked

When the ShowCheckBox property is true, gets

 

 

or sets whether the check box is checked.

 

CustomFormat

Gets or sets the custom date-time format.

 

Format

Gets or sets how the date-time value is

 

 

formatted in the control.

Public Properties

MaxDate

Gets or sets the maximum date-time value for

 

 

 

the control.

 

MinDate

Gets or sets the minimum date-time value for

 

 

the control.

 

ShowCheckBox

Gets or sets whether a check box displays to the

 

 

left of the selected date.

 

ShowUpDown

Gets or sets whether an up-down control is used

 

 

to adjust the date-time value.

 

Value

Gets or sets the DateTime value assigned to the

 

 

control. Default is the current date and time.

 

 

 

 

CloseUp

Occurs when the dropdown calendar is

 

 

dismissed and disappears.

Public Events

DropDown

Occurs when the dropdown calendar is shown.

 

 

 

FormatChanged

Occurs when the Format property changes.

 

ValueChanged

Occurs when the Value property changes.

 

 

 

11.3.1DATES AND TIMES

Our Photo Properties dialog with a DateTimePicker control in place is shown in figure 11.5. As you can see, the dropdown calendar control is displayed for the object.

DATES AND TIMES

367

Figure 11.5

The DateTimePicker shown here displays the Long date format, which is the default.

We can add this control to our dialog using the following steps. We will begin with the default display settings for this control, and look at how to modify these settings later in the section.

Set the version number of the MyPhotoAlbum application to 11.3.

REPLACE THE DATE TEXT BOX WITH A DATETIMEPICKER CONTROL

 

 

 

Action

Result

 

 

 

 

 

 

1

In the PhotoEditDlg.cs [Design]

 

 

window, delete the TextBox control

 

 

next to the Date Taken label.

 

 

 

 

 

 

 

2

Place a DateTimePicker control

Note: The location of this control is shown in

 

where the text box used to be.

figure 11.5.

 

 

Settings

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

(Name)

 

dtpDateTaken

 

 

 

TabIndex

 

5

 

 

 

 

 

 

 

 

 

3

Locate the ResetSettings method

protected override void ResetSettings()

 

in the MainForm.cs source file.

{

 

 

 

 

 

 

// Initialize the ComboBox settings

 

 

 

 

 

 

. . .

 

 

 

 

 

 

4

Set the Value property for the date

Photograph photo = _album.CurrentPhoto;

 

and time control.

if (photo != null)

 

How-to

 

 

 

 

 

 

 

{

 

Use the DateTaken property.

txtPhotoFile.Text = photo.FileName;

 

 

 

 

 

 

txtCaption.Text = photo.Caption;

 

 

 

 

 

 

dtpDateTaken.Value = photo.DateTaken;

 

 

 

 

 

 

cmbxPhotographer.SelectedItem

 

 

 

 

 

 

= photo.Photographer;

 

 

 

 

 

 

txtNotes.Text = photo.Notes;

 

 

 

 

 

 

}

 

 

 

 

 

 

}

 

 

 

 

 

 

 

368

CHAPTER 11 MORE CONTROLS

REPLACE THE DATE TEXT BOX WITH A DATETIMEPICKER CONTROL (continued)

 

Action

Result

 

 

 

5

Locate the SaveSettings method.

protected override void SaveSettings()

 

 

{

 

 

 

6

Set the DateTaken property to the

Photograph photo = _album.CurrentPhoto;

 

date-time value specified by the user.

if (photo != null)

 

 

 

 

{

 

 

photo.Caption = txtCaption.Text;

 

 

photo.DateTaken = dtpDateTaken.Value;

 

 

photo.Photographer

 

 

= cmbxPhotographer.Text;

 

 

photo.Notes = txtNotes.Text;

 

 

}

 

 

}

 

 

 

And there you have it. One DateTimePicker control ready to work. Compile and run the application, and set the dates for your photographs as appropriate. Make sure your albums preserve the selected date after exiting and restarting the program.

.NET Table 11.4 DateTimePickerFormat enumeration

The DateTimePickerFormat enumeration specifies how to display a date-time value in a

DateTimePicker control. This enumeration is part of the System.Windows.Forms namespace. For each value, the default setting for the U.S. English culture is provided. The format codes used here correspond to the codes supported by the DateTimeFormatInfo class.

Enumeration

Values

Custom

A custom format is used, as specified by the

 

CustomFormat property.

Long

The long date format is used. In Windows, this is typically

 

“dddd, MMMM dd, yyyy” for U.S. English environments.

 

This is the default value.

Short

The short date format is used. In Windows, this is

 

typically “MM/dd/yyyy” for U.S. English environments.

Time

The time format is used. In Windows, this is typically

 

“HH:mm:ss tt” for U.S. English environments.

 

 

You may have noticed that our control does not display the time. By default, the date and time control displays what .NET calls the long date. This includes the day of the week and month written out in the local language as well as the two-digit day and four-digit year. The format used by the control is specified by the Format property, using the DateTimePickerFormat enumeration described in .NET Table 11.4. As you can see from the table, various values allow either the date or time to be displayed in a format specified by the operating system.

11.3.2CUSTOMIZING A DATETIMEPICKER CONTROL

As can be seen in .NET Table 11.4, a custom display setting for the DateTimePicker control is used when the Format property is set to DateTimePicker-

DATES AND TIMES

369

Format.Custom. The CustomFormat property contains the string value to use in this case. A number of format codes are available within this string. These codes are managed by the sealed DateTimeFormatInfo class. The following table shows a number of these codes, along with some corresponding properties in the DateTimeFormatInfo class, which can be especially useful when operating in a multi-language environment. Consult the .NET documentation for the complete list of codes and additional information on the specified properties.

Date-time codes for the DateTimeFormatInfo class

Pattern

Description

Default U.S. English

DateTimeFormatInfo

Values

Property

 

 

 

 

 

 

d

Day of the month.

1 to 31

 

dd

Two-digit day of the month.

01 to 31

 

ddd

Abbreviated day of the week.

Sun to Sat

AbbreviatedDayNames

dddd

Full day of the week.

Sunday to Saturday

DayNames

M

Numeric month.

1 to 12

 

MM

Two-digit numeric month.

01 to 12

 

MMM

Abbreviated month name.

Jan to Dec

AbbreviatedMonthNames

MMMM

Full month name.

January to December

MonthNames

y

Year without century.

1 to 99

 

yy

Two-digit year without century.

01 to 99

 

yyyy

Four-digit century.

0001 to 9999

 

gg

Period or era, if any.

B.C. or A.D.

 

h

Hour on a 12-hour clock.

1 to 12

 

hh

Two-digit hour on a 12-hour clock.

01 to 12

 

H

Hour on a 24-hour clock.

1 to 24

 

HH

Two-digit hour on a 24-hour clock.

01 to 24

 

m

Minute.

0 to 59

 

mm

Two-digit minute.

00 to 59

 

s

Second.

0 to 59

 

ss

Two-digit second.

00 to 59

 

tt

AM/PM designator.

AM or PM

AMDesignator and

 

 

 

PMDesignator

:

Default time separator.

: ( a colon)

TimeSeparator

/

Default date separator.

/ (a slash)

DateSeparator

‘c

Displays the specified character. For

 

 

 

example, ‘s’ will display the

 

 

 

character s rather than the number

 

 

 

of seconds.

 

 

 

 

 

 

Let’s modify our date and time control to display a customized value. We will include both the date and time in the display.

370

CHAPTER 11 MORE CONTROLS

Соседние файлы в папке c#