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

DISPLAY A CUSTOM DATE-TIME VALUE IN THE DATETIMEPICKER CONTROL

 

 

 

Action

Result

 

 

 

 

 

 

1

Display the properties for the

 

 

DateTimePicker control in the

 

 

PhotoEditDlg.cs [Design] window.

 

 

 

 

 

 

 

2

Modify this control to display a custom

The control displays the new format within Visual

 

format string.

 

 

 

Studio.

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

CustomFormat

 

MM/dd/yy 'at' hh:mm tt

 

 

 

Format

 

Custom

 

 

 

 

 

 

 

 

If you compile and run these changes, you will find that the dropdown calendar still appears. The time values can be modified by hand or with the arrow keys. You might try using some alternate format strings, or setting the ShowUpDown property to true as a way to experiment with these customized settings.

The DateTimePicker class is great for displaying a single date-time value. When multiple dates or a range of dates are required, the MonthCalendar class can be used. We will discuss this control next.

More .NET As an alternative to a DateTimePicker control, another option here is to create separate controls for the month, day, and year, and if necessary the time of day. While the TextBox or ComboBox controls could be used for this purpose, you could also use the DomainUpDown and NumericUpDown controls. These controls are derived from the UpDownBase control, which in turn is based on the ContainerControl class presented in chapter 7.

The up-down controls present a text-box-like window that displays a range of values. The DomainUpDown control presents a string value taken from a collection of objects, while the NumericUpDown control presents a numeric value, optionally over a defined range.

For separate month, day, and year controls, the properties for the DateTimeFormatInfo class shown earlier in this section may be used to obtain the default set of month strings for display within a DomainUpDown control. The day and year values can be displayed in a NumericUpDown control, with the range set based on the current month and the requirements of the application.

DATES AND TIMES

371

11.4CALENDARS

Sometimes a single date will not do. A scheduling program, for example, might need to show a calendar with meeting days highlighted, or display a meeting that covers a range of dates. The MonthCalendar class allows one or more months to be displayed on a Form, with individual days highlighted or a range of days selected.

Since our PhotoAlbum class permits each photograph to specify its own date, it seems appropriate to demonstrate the calendar control by highlighting the days in a calendar on which a photograph was taken. We will do this by adding a second TabPage object to our MyAlbumEditor main window. The result of our changes is shown in figure 11.6. Note how some dates are in bold to indicate one or more photographs were taken that day. If the user clicks on a date, a context menu pops up containing the corresponding photographs. When a photograph is selected from this context menu, the properties for that photograph are displayed.

The interface in figure 11.6 provides a very different view of our album. While the order of photographs in the album is not apparent, the specific days that a collection of pictures was taken is immediately available.

This section will discuss the month calendar control in general and add the control to a new tab page in our application. We will discuss how to bold the dates when photographs were taken, and how to process and respond to mouse clicks made within the control.

Figure 11.6

The MonthCalendar control will automatically display multiple months as it is resized.

11.4.1ADDING A MONTHCALENDAR CONTROL

An overview of the MonthCalendar class is provided in .NET Table 11.5. This class handles the entire range of dates possible in DateTime objects, which is basically any date with a four-digit century. This class is a good way to display a series of dates related to an object or collection of objects.

372

CHAPTER 11 MORE CONTROLS

.NET Table 11.5 MonthCalendar class

The MonthCalendar class represents a control that displays one or more months to the user. Days in each month can be displayed in bold, and the user can select single or multiple dates. 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.

 

AnnuallyBoldedDates

Gets or sets an array of DateTime objects that

 

 

indicate which days to show in bold on an annual

 

 

basis.

 

BoldedDates

Gets or sets an array of DateTime objects of specific

 

 

dates to show in bold.

 

MaxDate

Gets or sets the maximum date. The user will not be

 

 

able to display months occurring after this date.

 

MaxSelectionCount

Gets or sets the maximum number of dates that can

 

 

be selected in the control. Defaults to seven (7).

Public

ScrollChange

Gets or sets the number of months to scroll per click

 

of a scroll button. Defaults to one (1).

Properties

 

 

 

 

SelectionRange

Gets or sets the range of dates selected in the

 

 

control.

 

SelectionStart

Gets or sets the initial date of the range selected in

 

 

the control.

 

ShowToday

Gets or sets whether to display the TodayDate

 

 

value at the bottom of the control.

 

ShowTodayCircle

Gets or sets whether the TodayDate value is circled.

 

TodayDate

Gets or sets the DateTime value used as today’s

 

 

date.

 

 

 

 

AddAnnuallyBoldedDate

Adds a day to display in bold on an annual basis.

 

GetDisplayRange

Retrieves the range of dates displayed by the

 

 

control.

Public

HitTest

Determines which aspect of the month calendar

Methods

 

control is located at a specific point.

 

RemoveBoldedDate

Removes a specific date from the list of nonrecurring

 

 

bolded dates.

 

SetDate

Selects the given date in the control.

 

 

 

 

DateChanged

Occurs when the current date in the control is

Public

 

modified, such as when a new month is displayed.

 

 

Events

DateSelected

Occurs when the dates selected in the control are

 

 

 

modified.

 

 

 

In our case, we will display the dates associated with a collection of photographs. Let’s begin by adding a new TabPage containing a MonthCalendar control to our form.

CALENDARS

373

Set the version number of the MyAlbumEditor application to 11.4.

 

 

 

 

 

CREATE THE DATES TAB PAGE

 

 

 

 

 

 

 

 

 

 

 

 

Action

Result

 

 

 

 

 

 

 

 

 

1

 

In the MainForm.cs [Design]

 

 

 

window, add a second tab page

 

 

 

to the TabControl object.

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

Value

 

 

 

 

(Name)

tabDates

 

 

 

 

Text

Dates

 

 

 

 

 

 

 

 

 

 

2

 

Add a MonthCalendar control

 

 

 

to this page.

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

 

(Name)

 

monthCalDates

 

 

 

 

Dock

 

Fill

 

 

 

MaxSelection-

 

1

 

 

 

 

 

 

Count

 

 

 

 

 

 

 

 

ShowToday

 

False

 

 

 

 

 

 

 

 

 

Note: Your MonthCalendar control will circle the

 

 

 

 

 

 

 

 

current date, which is likely not the date shown in

 

 

 

 

 

 

 

 

the graphic.

 

 

 

 

 

 

 

 

 

You will note that the Dock property for our month calendar object is set to Fill. This ensures that the number of months displayed will expand to fill the entire tab page as the form is enlarged. As we will see in the next section, months before the MinDate property value and after the MaxDate value will not be accessible from this control.

11.4.2INITIALIZING A CALENDAR

Now that our MonthCalendar control is on the form, we can hook it up to our PhotoAlbum class. We do not want to initialize the calendar needlessly, so we will only do so when the Dates tab is displayed. By the same token, we do not want to initialize the lstPhotos list box needlessly, so we need to ensure that this only occurs when the Photos tab is displayed. Since we used the method UpdateList for our list box, we will create an UpdateCalendar method to initialize and update our

MonthCalendar control.

The following steps are required for this change:

374

CHAPTER 11 MORE CONTROLS

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