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

The solution is to use cut and paste just like you might when moving text around in a document. We will cut the controls from the group box and then paste them inside of a tab page. The steps required are described in the following table:

 

REPLACE THE GROUPBOX WITH A TABPAGE

 

 

 

 

Action

Result

 

 

 

1

In the MainForm.cs [Design]

 

 

window, highlight the set of

 

 

controls inside the Photographs

 

 

group box.

 

 

How-to

 

 

a. Click the ListBox control.

 

 

b. Hold down the Ctrl key and

 

 

click the four Button controls.

 

 

Alternately

 

 

Click inside the GroupBox

 

 

control and drag a focus

 

 

rectangle to include all five

 

 

controls.

 

 

 

 

2

Cut the selected controls to the

 

 

Clipboard.

 

 

How-to

 

 

Select Cut from the Edit menu.

 

 

Alternately

 

 

Use the keyboard shortcut

 

 

Ctrl+X.

 

 

 

 

3

Delete the Photographs group

 

 

box control from the form.

 

 

 

 

4

Drag a TabControl object onto

 

 

the form, and resize it to be

 

 

about the same size as the

 

 

deleted Photographs group box.

 

 

 

 

364

CHAPTER 11 MORE CONTROLS

REPLACE THE GROUPBOX WITH A TABPAGE (continued)

 

 

 

 

Action

 

 

Result

 

 

 

 

 

 

 

5

 

Add a TabPage object within the

 

 

 

tab control.

 

 

 

 

 

 

 

How-to

 

 

 

 

 

 

 

a. Right-click the TabControl

 

 

 

object.

 

 

 

 

 

 

 

b. Select Add Tab from the

 

 

 

menu.

 

 

 

 

 

 

 

Alternately

 

 

 

 

 

 

 

You can use the TabPages

 

 

 

property of the tab control to

 

 

 

display the TabPage Collection

 

 

 

Editor dialog.

 

 

 

 

 

 

 

 

 

 

6

 

Insert the controls inside the

 

 

 

TabPage control. Resize and

 

 

 

position the controls if

 

 

 

 

 

necessary.

 

 

 

 

 

 

 

How-to

 

 

 

 

 

 

 

a. Click inside the TabPage

 

 

 

object to make it the active

 

 

 

control.

 

 

 

 

 

 

 

b. Select Paste from the Edit

 

 

 

menu.

 

 

 

 

 

 

 

Alternately

 

 

 

 

 

 

 

Click inside the control and use

 

 

 

the keyboard shortcut Ctrl+V.

 

 

 

 

 

 

 

 

7

 

Set the properties for the tab

 

 

 

control and page.

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

Control

 

Property

 

Value

 

 

 

TabControl

 

(Name)

 

tcPhotos

 

 

 

 

 

Alignment

 

Left

 

 

 

 

 

Anchor

 

Top,

 

 

 

 

 

 

 

Bottom,

 

 

 

 

 

 

 

Left, Right

 

 

 

TabPage

 

(Name)

 

tabPhotos

 

 

 

 

 

Text

 

Photos

 

 

 

 

 

 

 

 

 

 

TAB PAGES

365

Pho-

REPLACE THE GROUPBOX WITH A TABPAGE (continued)

 

Action

Result

 

 

 

8

Manually reestablish the event

The event handlers for the controls are assigned to the

 

handlers for the controls. This

appropriate events.

 

includes the DoubleClick,

Note: This step is required whenever a control is

 

DrawItem, MeasureItem, and

 

cut from one location and pasted into another. The

 

SelectedIndexChanged event

 

event handlers are not preserved, although the

 

handlers for the list box, and the

 

properties of the controls are.

 

Click handlers for each of the

 

 

 

four button controls.

 

 

How-to

 

 

Use the Events listing in the

 

 

Properties window, and select

 

 

the existing methods from the

 

 

appropriate dropdown lists.

 

 

 

 

The Photographs group box is now replaced with a Photos tab page. This tab is aligned on the left side of the tab control. The Alignment property uses the TabAlignment enumeration, with possible values Top, Bottom, Left, and Right.

As you may have noticed, when the Alignment property for a tab control is Left or Right, the Multiline property is automatically set to true. Compile and run the application to make sure the controls still behave as expected, including multiple selection and the owner-drawn list feature via the Thumbnails menu.

We will add a second TabPage later in the chapter to display the set of dates associated with the album in a calendar format. This will enable a discussion of the MonthCalendar class. Before we can do this, we will first provide the appropriate support for the DateTaken property of the Photograph class.

11.3DATES AND TIMES

We will return to tab pages and our MyAlbumEditor application in a moment. In this section we finally preserve the Date Taken value entered by the user in our toEditDlg form. As you may recall, in chapter 9 we intentionally ignored this value to avoid converting the user-entered string value into a date. At the time we said there was a better way to deal with date constructs. In this section we finally see exactly what this looks like.

Dealing with dates and times is one of those issues that prevent some programmers from getting a good night’s sleep. With 3600 seconds in an hour, and 24 hours in a day, and different days per month, and leap years almost but not quite every four years, it’s no wonder. Fortunately, most languages and environments these days provide direct support for date-time values to simplify handling of these constructs. In the

.NET Framework, this support extends to Windows Forms controls as well.

In chapter 5 we saw how the DateTime structure is used to represent a date-time value within a program. In this section we will look at representing a date-time value

366

CHAPTER 11 MORE CONTROLS

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