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

The Control class extends the Component class we saw in chapter 3. All controls are components, and therefore support the IComponent and IDisposable interfaces. Controls can act as containers for other controls, although not all controls actually do so. The premier example of such a container is the Form class, which we have been using for our application window all along. The class hierarchy for the Form class is discussed in chapter 7.

All controls are also disposable. When you are finished with a control, you should call the Dispose method inherited from the Component class to clean up any nonmemory resources used by the control.

The Control class forms the basis for all windows controls in .NET, and provides many of the properties, methods, and events we have already seen such as the Left, Top, Width, and Height properties, the Invalidate method, and the Click event. An overview of the Control class is provided in .NET Table 4.1. Note that only a portion of the many members of this class are shown in the table. Consult the online documentation for the complete list of members.

The StatusBar class is just one of many controls derived from the Control class. We will look at the StatusBar class in more detail in a moment, and other control classes throughout the rest of the book.

4.2THE STATUSBAR CLASS

Now that we have seen the class hierarchy, let’s turn our attention to the StatusBar class itself. Typically, an application has only one status bar, although its contents may change as the application is used in different ways. Two types of information are normally displayed on a status bar.

Simple text—the status bar can display a text string as feedback on the meaning of menu commands and toolbars. This is often referred to as flyby text since it displays as the cursor moves over, or flies by, the associated control. A simple string can also display status information on what the application is currently doing. For example, we will use the status bar to display a message while the application is loading a selected image. On a slower machine or for a large image, this will tell our user that the application is busy and currently unavailable.

State or attribute information—another type of data often provided is relevant information about the application or an object displayed by the application. This information is usually divided into separate areas called status bar panels (or status bar indicators or panes). Such information can include both text and graphical data. In this chapter, we will use a status bar panel to display the image size in pixels of the displayed image.

This section will implement the first type of information to display the status bar shown in figure 4.3. As before, this chapter builds on the application constructed in the previous chapter.

THE STATUSBAR CLASS

105

Figure 4.3

The status bar shown here uses the Text property of the StatusBar class to display a string to the user.

4.2.1ADDING A STATUS BAR

As you might expect, a status bar can be added to our application in Visual Studio by dragging one from the Toolbox window onto the form.

Set the version number of the application to 4.2.

 

 

ADD A STATUS BAR

 

 

 

 

Action

Result

 

 

 

1

Place a status bar at the base

The new status bar appears in the designer window. For lack

 

of the MyPhotos application.

of a better choice, we’ll use the default name statusBar1.

 

How-to

 

 

a. Display the MainForm.cs

 

 

[Design] window.

 

 

b. Drag a StatusBar object

 

 

from the Toolbox window

 

 

onto the form.

 

 

 

 

2

Set the Text property for the

 

 

StatusBar control to

 

 

“Ready.”

 

 

 

 

106

CHAPTER 4 STATUS BARS

Before we interact with our new status bar, let’s take a look at the code so far. An excerpt of the code in our MainForm.cs source file is shown below.

. . .

private System.Windows.Forms.StatusBar statusBar1;

. . .

private void InitializeComponent()

{

. . .

this.statusBar1 = new System.Windows.Forms.StatusBar();

. . .

//

// statusBar1

//

this.statusBar1.Location = new System.Drawing.Point(0, 233); this.statusBar1.Name = "statusBar1";

this.statusBar1.Size = new System.Drawing.Size(292, 20);

this.statusBar1.TabIndex = 2;

b Set the tab order

this.statusBar1.Text = "Ready";

for status bar

. . .

 

pbxPhoto.Dock = System.Windows.Forms.DockStyle.Fill;

. . .

 

this.Controls.AddRange(new System.Windows.Forms.Control[] {

 

this.statusBar1,

 

this.pbxPhoto});

}

Add the status

 

bar before the

 

picture boxc

This looks very similar to code we have seen before. As usual, though, there are some points worth highlighting.

bThis line is a little strange. You do not normally tab into a status bar, so why set a tab index? Visual Studio does this to ensure that each control has a unique index, but it does not mean that you can tab to the status bar control. By default, the StatusBar sets the TabStop property (inherited from the Control class) to false. So the status bar is not a tab stop (by default), even though Visual Studio sets a TabIndex for it.

cIf you recall, the order in which controls are added establishes the z-order stack (which controls are in front or behind the others). This is important here since the pbxPhoto control takes up the entire window (with Dock set to Fill). By adding the status bar first, this insures this control is on top, docked first, and therefore visible. In the Forms

Designer window, you can right-click a control to select the Bring to Front or Send to Back item and modify the z-order.1 You might try this to verify that the status bar is hidden if you run the application with pbxPhoto at the top of the z-order.

1You can also change the order in which controls are added by rearranging their order in the InitializeComponent method. While Microsoft recommends against this, it does work.

THE STATUSBAR CLASS

107

d Set the Dock property I know, there is no number 3 in the code. I’m just trying to see if you’re paying attention. The default setting for the Dock property in the Control class is DockStyles.None. The StatusBar class overrides this setting to use DockStyles.Bottom by default. This ensures the status bar appears docked at the bottom of the form. Since this is the default, Visual Studio does not set this value in the code, so there is no number 3.

A summary of the StatusBar class is shown in .NET Table 4.2. One feature noticeably missing from the StatusBar class is flyby text. In the MFC classes, menu and toolbar objects can set help messages that appear in the status bar as the cursor passes over the corresponding control. This feature may well be included in a future release of the .NET Framework.

.NET Table 4.2 StatusBar class

The StatusBar class is a control used to show a status bar on a form. This class can display either a textual string or a collection of panels in the form of StatusBarPanel objects. Whether the text or panels appear is determined by the value of the ShowPanels property. The StatusBar 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 the Control class, and .NET Table 4.3 on page 116 for more information on the StatusBarPanel class.

 

Dock

Gets or sets the dock setting for the control. The

 

(inherited from

default value for status bars is DockStyles.Bottom.

 

 

 

Control)

 

 

Panels

Gets the StatusBarPanelCollection class

 

 

containing the set of StatusBarPanel objects

 

 

managed by this status bar.

 

ShowPanels

Gets or sets whether the panels (if true) or text (if

 

 

false) should be displayed on the status bar.

Public Properties

 

Defaults to false.

 

 

 

SizingGrip

Gets or sets whether a sizing grip should be

 

 

displayed in the corner of the status bar. This grip can

 

 

be used to resize the form. Defaults to true.

 

TabStop

Gets or sets whether the control is a tab stop on the

 

(inherited from

form. The default value for status bars is false.

 

Control)

 

 

Text

Gets or sets the text for the status bar. This is

 

(inherited from

displayed on the status bar only if ShowPanels is set

 

Control)

to false.

 

 

 

 

DrawItem

Occurs when an owner-drawn status bar panel must

Public Events

 

be redrawn.

 

 

 

PanelClick

Occurs when a panel on the status bar is clicked.

 

 

 

108

CHAPTER 4 STATUS BARS

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