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

4.2.2ASSIGNING STATUS BAR TEXT

In our application, we will add some helpful text when an image is loaded and displayed. This will let the user know when a file is loading, and when it is complete.

 

 

SET THE STATUS BAR TEXT

 

 

 

 

Action

Result

 

 

 

1

Locate the menuLoad_

protected void menuLoad_Click

 

Click method in the

(object sender, System.EventArgs e)

 

MainForm.cs code window.

{

 

OpenFileDialog dlg = new OpenFileDialog();

 

 

 

 

. . .

 

 

 

2

Define a status bar

The changes to the try-catch block in this method are shown

 

message before and after

in bold.

 

an image is loaded.

try

 

 

 

 

{

 

 

statusBar1.Text

 

 

= "Loading " + dlg.FileName;

 

 

pbxPhoto.Image

 

 

= new Bitmap(dlg.OpenFile());

 

 

statusBar1.Text

 

 

= "Loaded " + dlg.FileName;

 

 

}

 

 

catch (Exception ex)

 

 

{

 

 

statusBar1.Text

 

 

= "Unable to load " + dlg.FileName;

 

 

MessageBox.Show(

 

 

"Unable to load file: "

 

 

+ ex.Message);

 

 

}

 

 

 

Whether or not the user sees the "Loading…" message depends on the speed of his or her machine and the size of the image. After an image is successfully loaded, the "Loaded…" message displays as per figure 4.5 at the beginning of this section.

Since the assignment of the status bar text occurs within a try block, it is important to consider the implications of an exception occurring. If an exception is thrown while preparing the file for display in the PictureBox control, then the “Loading…” line will still be present on the status bar. To make sure this doesn’t happen, we assign the status bar text to a more appropriate value in our exception handler.

Of course, other text messages could be added to our application as well. We will see additional examples as we progress through the book.

TRY IT! You can implement flyby, or temporary, help text for menu items using the Select event in the MenuItem class. This event occurs when the cursor is placed over the menu. Handle this event for the Load menu to display the text “Loads a photo to display in the window” on the status bar whenever the cursor hovers over this menu item.

The Form class provides the MenuStart and MenuComplete events to capture when the menu associated with a form has and then loses focus.

THE STATUSBAR CLASS

109

Figure 4.4 Status bar panels can be displayed with no border, a sunken border, or a raised border (not shown).

You can use these events to enable and disable the display of help text in the status bar. The easiest way to do this here is to set the Text property of the status bar to empty whenever the menu loses focus. Either handle the event in the MainForm class or override the protected OnMenuComplete method in your Form class.

In a form with status bar panels, the MenuStart and MenuComplete events can be used to toggle between displaying the panels and displaying flyby text on the corresponding status bar. The panels are hidden in the handler for the MenuStart event, and redisplayed in the handler for the

MenuComplete event.

4.3STATUS BAR PANELS

Now that we have seen how to add a status bar and display simple text, we can talk about status bar panels. Panels provide a nice way to encapsulate a specific nugget of information in a single location. As we will see, panels can present both text and graphical information to your users.

When designing an application, do not crowd so many panels into your status bar that it becomes cluttered and unusable. Make sure the information you provide is desired and useful to your users. An example of an extraneous panel might be the book and pencil graphic that animates whenever you type into Microsoft Word. A pretty little graphic, but who needs to be told when they are typing? Keep your panel information to a minimum, and your users will thank you.

For our application, let’s add three panels to provide some information on the displayed image. These panels are shown

in figure 4.4. The first panel will display the filename of the image; the second the image’s dimensions in pixels; and the third will be an owner-drawn panel displaying the percentage of the image currently shown. The following table summarizes these panels. We will use the sbpnl prefix to identify these variables as StatusBarPanel objects.

110

CHAPTER 4 STATUS BARS

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