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

Note how the photograph’s base file name is used as the text for each tab page, and how the full file path appears as a tool tip associated with each tab.

Figure 11.3

Each tab page in the tab control for this window displays an image stretched to fit a PictureBox control.

You may think that we need to add a new Form class to our project using Visual Studio .NET. This would certainly work, but you do not need a new file in Visual Studio every time a new form is required. Instead, we will create this form by hand. We will add a new MenuItem object to the context menu built in section 10.5 to provide access to this new form.

This section creates the new menu item and associated Click handler. The next section discusses tab pages, and will continue the implementation of this new form.

Set the version number of the MyAlbumEditor application to 11.1.

 

 

 

ADD A NEW CONTEXT MENU ITEM

 

 

 

 

 

 

 

 

Action

Result

 

 

 

 

 

 

1

In the MainForm.cs [Design] window,

 

 

add a menu separator to the

 

 

ContextMenu object on the form.

 

 

 

 

 

 

 

2

Add a new Images menu.

 

 

 

Settings

 

 

 

 

 

 

 

 

 

Property

Value

 

 

 

(Name)

menuImages

 

 

 

Text

&Images…

 

 

 

 

 

 

 

3

Add a Click handler for this menu.

private void menuImages_Click

 

 

 

 

 

(object sender, System.EventArgs e)

 

 

 

 

 

{

 

 

 

 

 

}

 

 

 

 

 

 

TAB CONTROLS

357

As you may recall, this context menu is associated with the lstPhotos control. Whenever the user right-clicks on list and selects our new item, the menuImages_Click handler will execute. In this handler we will create our new form.

The steps to create a tab control on a form programmatically are shown in the following table. The next section will add the individual tab pages to this form.

IMPLEMENT THE CLICK EVENT HANDLER TO CREATE A NEW FORM

 

 

 

Action

 

 

 

Result

 

 

 

 

 

 

 

 

 

4

Create a new Form in the Click

private void menuImages_Click

 

handler.

 

 

 

(object sender, System.EventArgs e)

 

 

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

 

 

Form imagesDlg = new Form();

 

 

 

 

 

 

 

 

 

5

Create a TabControl object for

TabControl tcImages = new TabControl();

 

the form.

 

 

 

 

 

 

 

 

 

 

 

 

 

6

Suspend the layout of both

imagesDlg.SuspendLayout();

 

objects while the individual tab

tcImages.SuspendLayout();

 

pages are created.

 

 

 

// Create a tab page for each photo

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

// (see next section)

 

 

 

 

 

 

 

 

 

7

Initialize the tab control.

tcImages.Dock = DockStyle.Fill;

 

 

 

Settings

 

 

 

tcImages.HotTrack = true;

 

 

 

 

 

 

tcImages.ShowToolTips = true;

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

 

Value

 

 

 

 

Dock

 

 

Fill

 

 

 

 

HotTrack

 

 

True

 

 

 

 

ShowToolTips

 

 

True

 

 

 

 

 

 

 

 

 

 

 

8

Initialize the form to contain the

imagesDlg.Controls.Add(tcImages);

 

tab control.

 

 

 

imagesDlg.ShowInTaskbar = false;

 

 

 

 

 

 

 

 

 

imagesDlg.Size = new Size(400, 300);

 

 

 

Settings

 

 

 

imagesDlg.Text = "Images in "

 

 

 

 

 

 

 

 

 

+ Path.GetFileName(_album.FileName);

 

 

 

Property

 

Value

 

 

 

 

 

 

 

 

ShowInTaskbar

 

False

 

 

 

 

Size

 

400, 300

 

 

 

 

 

 

Text

 

as shown

 

 

 

 

 

 

 

 

 

 

 

9

Resume layout of the container

tcImages.ResumeLayout();

 

controls.

 

 

 

imagesDlg.ResumeLayout();

 

 

 

 

 

 

 

 

 

10

Display the form as a modal

imagesDlg.ShowDialog();

 

dialog.

 

 

 

Note: We ignore the value returned by the ShowDia-

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

log method.

 

 

 

 

 

 

 

 

 

11

Dispose of the form.

imagesDlg.Dispose();

 

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

As you can see, this code creates a Form with a single TabControl object docked to fill the entire window area. The hot tracking feature causes a tab’s text to change color as the mouse passes over the tab. Both this feature and tool tips are enabled for all tab pages in the control.

358

CHAPTER 11 MORE CONTROLS

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