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

StatusBarPanel objects for our application

Panel Name

Contents

Notes

sbpnlFileName

The file name of the image

 

currently displayed.

Later in the book, we will change this panel to display a user-supplied caption. For now, the file name of the image will suffice.

sbpnlImageSize

The dimensions of the

We will write the text for this panel as “width x

 

image in pixels.

height,” as shown in figure 4.4.

sbpnlImagePercent

The percentage of the

The percent of image that is shown only

 

image currently shown.

changes in the Actual Size display mode. We

 

 

will show a graphical bar taking up the

 

 

equivalent percent of the panel.

 

 

 

In this section we will add the new panels to our status bar, and define some text for the first two panels. The final panel is an owner-drawn panel, and is the subject of section 4.4.

4.3.1Adding panels to a status bar

Let’s begin by adding our three panels to the status bar. In Visual Studio, panels are not added via the Toolbox, but rather through the Panels item in the Property window. If you are not using Visual Studio, you can create StatusBarPanel objects like you would any other object for your form.

Set the version number of the application to 4.3.

ADD STATUS BAR PANELS

Action

Result

1In the designer window, display the Properties window for the statusBar1 control.

STATUS BAR PANELS

111

ADD STATUS BAR PANELS (continued)

 

 

 

Action

Result

 

 

 

 

 

 

 

 

 

2

Display the StatusBarPanel

The StatusBarPanel Collection Editor dialog appears, where

 

Collection Editor dialog for

panels for the status bar can be added and removed.

 

the status bar.

 

 

 

How-to

 

 

 

 

 

 

 

a. Click the Panels entry in

 

 

 

 

the Properties window.

 

 

 

b. Click the small button

 

 

 

 

that appears.

 

 

 

Note: The Panels prop-

 

 

 

erty holds the collection of

 

 

 

panels for the status bar.

 

 

 

 

 

 

 

 

 

 

3

Add a new panel for the

The first panel (number 0) is added to the dialog. Panels are

 

control.

 

 

 

 

shown in the Members column on the left, and properties are

 

 

How-to

 

 

 

 

shown on the right.

 

 

 

 

 

 

The dialog after all three panels have been added is shown

 

 

a. In the Editor window,

 

 

 

click the Add button.

below in Step 5.

 

 

b. Set the panel’s properties

 

 

 

 

as shown below.

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

 

(Name)

 

sbpnlFileName

 

 

 

 

AutoSize

 

Spring

 

 

 

 

BorderStyle

 

None

 

 

 

 

ToolTipText

 

Image File

 

 

 

 

 

 

Name

 

 

 

 

 

 

 

 

 

 

4

Add the second panel.

The second panel is added as panel number 1 in the dialog.

 

 

 

Settings

 

Note: The arrow buttons in the center of the Editor dialog

 

 

 

Property

 

Value

are used to alter the order in which panels will appear.

 

 

 

(Name)

sbpnlImageSize

We will use this feature in chapter 6 when we add an

 

 

 

additional panel to our status bar.

 

 

 

AutoSize

 

Contents

 

 

 

 

 

 

 

 

ToolTipText

 

Image Size

 

 

 

 

 

 

 

 

 

 

112

CHAPTER 4 STATUS BARS

b Begin panel initialization

ADD STATUS BAR PANELS (continued)

Action

Result

5 Add the third panel (panel 2).

Settings

Property

Value

(Name)

sbpnlImage-

 

Percent

Style

OwnerDraw

ToolTipText

Percent of

 

Image Shown

Width

75

 

 

As is our custom, let’s look at an excerpt of the code generated by these actions.

private System.Windows.Forms.StatusBarPanel sbpnlImagePercent; private System.Windows.Forms.StatusBarPanel sbpnlImageSize; private System.Windows.Forms.StatusBarPanel sbpnlFileName;

. . .

private void InitializeComponent()

{

. . .

this.sbpnlFileName = new System.Windows.Forms.StatusBarPanel(); this.sbpnlImageSize = new System.Windows.Forms.StatusBarPanel(); this.sbpnlImagePercent = new System.Windows.Forms.StatusBarPanel(); ((System.ComponentModel.ISupportInitialize)

(this.sbpnlFileName)).BeginInit(); ((System.ComponentModel.ISupportInitialize) =

(this.sbpnlImageSize)).BeginInit(); ((System.ComponentModel.ISupportInitialize)

(this.sbpnlImagePercent)).BeginInit();

. . .

 

//

Set panel

// sbpnlFileName

//

properties c

 

this.sbpnlFileName.AutoSize =

 

System.Windows.Forms.StatusBarPanelAutoSize.Spring;

this.sbpnlFileName.BorderStyle =

System.Windows.Forms.StatusBarPanelBorderStyle.None;

this.sbpnlFileName.Text = "statusBarPanel1"; this.sbpnlFileName.ToolTipText = "Image File Name"; d Set tool tip text

STATUS BAR PANELS

113

//

 

 

 

 

 

 

 

 

 

 

// sbpnlImageSize

Set panel

 

c

//

properties

 

this.sbpnlImageSize.AutoSize =

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

System.Windows.Forms.StatusBarPanelAutoSize.Contents;

 

 

 

 

 

 

 

 

 

 

this.sbpnlImageSize.Text = "statusBarPanel2";

 

 

 

 

 

 

 

 

 

 

this.sbpnlImageSize.ToolTipText = "Image Size";

 

 

 

 

 

 

 

 

 

 

this.sbpnlImageSize.Width = 97;

 

 

 

 

 

 

 

 

 

 

//

Set panel

 

 

 

 

 

// sbpnlImagePercent

 

 

 

 

 

properties c

 

 

 

 

 

//

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

this.sbpnlImagePercent.Style =

 

 

 

 

 

 

 

 

 

 

System.Windows.Forms.StatusBarPanelStyle.OwnerDraw

 

 

 

 

 

 

 

 

 

 

 

 

 

 

this.sbpnlImagePercent.Text = "statusBarPanel3";

 

 

 

 

 

 

 

 

 

 

this.sbpnlImagePercent.ToolTipText = "Percent of Image Shown";

 

 

 

 

 

this.sbpnlImagePercent.Width = 75;

Update StatusBar

 

 

 

 

 

. . .

 

 

 

 

 

this.statusBar1.Panels.AddRange(new

object

e

 

 

 

 

System.Windows.Forms.StatusBarPanel[] {

 

 

 

 

 

 

 

 

 

 

this.sbpnlFileName,

 

 

 

 

 

this.sbpnlImageSize,

 

 

 

 

 

this.sbpnlImagePercent});

 

. . .

 

 

 

 

 

 

 

 

 

 

((System.ComponentModel.ISupportInitialize)

 

 

 

 

 

 

 

 

 

 

(this.sbpnlFileName)).EndInit();

 

 

 

 

 

 

 

 

 

 

((System.ComponentModel.ISupportInitialize)

 

 

 

 

 

 

 

 

 

 

(this.sbpnlImageSize)).EndInit();

 

 

 

 

 

 

 

 

 

 

((System.ComponentModel.ISupportInitialize)

 

 

 

 

 

 

 

 

 

 

(this.sbpnlImagePercent)).EndInit();

 

 

 

 

 

 

 

 

 

 

. . .

 

 

 

 

 

 

 

 

 

 

}

Some of the properties here are a little different than we have seen before. The StatusBarPanel is not a control, but rather a Component object similar to our menu object.

bThe StatusBarPanel object must be fully initialized before it can be used within the StatusBar control. The ISupportInitialize interface specifies that this object uses a simple transacted notification for batch initialization. When creating such an object, use of the BeginInit and EndInit methods supported by this interface should be used to ensure proper initialization.

cSince panels exist within a status bar control, properties exist to indicate how to draw the panel within the containing control. See .NET Table 4.3 for a summary of all properties in the StatusBarPanel class. Some properties used here are briefly explained in the following list.

AutoSize indicates whether the panel is automatically sized within the status

bar, and if so how. This property uses the StatusBarPanelAutoSize enumeration, with the following values:

114

CHAPTER 4 STATUS BARS

StatusBarPanelAutoSize Value

Description

 

 

Contents

The width of the panel expands or contracts to fit

 

the actual contents of the panel.

None

The width of the panel is fixed based on the Width

 

property setting. This is the default.

Spring

The width of the panel expands or contracts to share

 

the available space with other panels that have the

 

Spring size setting.

 

 

BorderStyle indicates the type of border to use for the panel, taken from the

StatusBarPanelBorderStyle enumeration:

StatusBarPanelBorderStyle Value

Description

 

 

None

The panel is displayed with no border.

Raised

The panel is displayed with a raised border.

Sunken

The panel is displayed with a sunken border. This is

 

the default.

 

 

Style indicates how the panel should be drawn, taken from the StatusBarPanelStyle enumeration:

StatusBarPanelStyle Value

Description

 

 

OwnerDraw

The panel is drawn by the owner, using the DrawItem

 

event in the StatusBar class.

Text

The panel is drawn by the system using the Text

 

property of the panel.

 

 

As you can see from the code, two of our panels display text in the status bar, and one of them is an owner-drawn panel. Each of the AutoSize values are used.

dPanels provide a built-in tool tip mechanism in the ToolTipText property. These appear when the cursor hovers over the corresponding panel. We will look at the ToolTips class in chapter 13 as a way to support tool tips for classes derived from the

Control object.

eFinally, note the changes to our statusBar1 variable. The set of panels is added to the Panels property using the AddRange method.

this.statusBar1.Panels.AddRange(new

System.Windows.Forms.StatusBarPanel[] {

this.sbpnlFileName,

this.sbpnlImageSize,

this.sbpnlImagePercent});

STATUS BAR PANELS

115

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