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

These changes implement the correct functionality for the button. When the button is pushed, a Click event for the menuPixelData menu is performed, which displays the dialog. When the button is unpushed,1 the dialog is hidden using the Hide method. In this later case we ensure that the dialog exists and is shown before trying to hide it. The AssignPixelToggle method adjusts the image and the tool tip to reflect the new state of the button.

You can run the program to see the button in action. If you do, you may notice that there are two problems we still need to address:

The button is not pushed when the pixel data dialog is displayed using the View menu item.

The button is not unpushed when the dialog is closed manually.

For the first problem, we simply need to adjust the button in the Click event handler for this menu. Let’s do this before we discuss the second problem.

UPDATE THE TOGGLE BUTTON WHEN THE PIXEL DATA MENU IS SELECTED

 

Action

Result

 

 

 

4

Locate the menuPixelData_Click

private void menuPixelData_Click

 

event handler in the MainForm.cs

(object sender, System.EventArgs e)

 

code window.

{

 

 

 

 

 

5

Update this method to adjust the

if (_dlgPixel == null

 

toggle button settings.

|| _dlgPixel.IsDisposed)

 

 

{

 

 

_dlgPixel = new PixelDlg();

 

 

_dlgPixel.Owner = this;

 

 

}

 

 

_nPixelDlgIndex = _album.CurrentPosition;

 

 

Point p = pnlPhoto.PointToClient(

 

 

Form.MousePosition);

 

 

UpdatePixelData(p.X, p.Y);

 

 

AssignPixelToggle(true);

 

 

_dlgPixel.Show();

 

 

}

 

 

 

Our second problem, that of the user closing the pixel data dialog by hand, is more problematic. Since this dialog is a nonmodal window, this dialog can be closed at any time. So we need a mechanism for notifying our main window whenever the dialog is closed.

If you recall, and as shown in step 5 in the previous table, the MainForm form is defined as the owner of the PixelDlg form. This ensures that both windows are

1I know, I know. There is no such word as “unpushed.” You know what I mean. I thought about the word “released,” but unpushed seems much more packed with meaning.

428

CHAPTER 13 TOOLBARS AND TIPS

shown when either window is displayed or minimized. We can take advantage of this relationship to ensure that our main window is notified when the pixel dialog is closed.

The trick is to force the MainForm window to activate whenever the PixelDlg dialog is closed. Our main form will then receive an Activated event, at which time we can update our button. Since the MainForm class derives directly from Form, we can handle this event by overriding the protected OnActivated method.

The following steps implement this mechanism.

UPDATE THE TOGGLE BUTTON WHEN THE PIXELDLG FORM IS CLOSED

 

Action

Result

 

 

 

6

In the PixelDlg.cs code window,

protected override void OnClosing

 

override the OnClosing method to

(CancelEventArgs e)

 

activate the owner of the dialog, if any.

{

 

Visible = false;

 

 

 

Note: Since the dialog may not be

if (this.Owner != null)

 

fully closed here if the Main-

Owner.Activate();

 

 

 

Form.OnActivated method runs

base.OnClosing(e);

 

immediately, we set the Visible

}

 

property to false to ensure the cor-

 

 

rect behavior occurs.

 

 

Also note that overriding the

 

 

OnClosed method instead does not

 

 

work because the Owner property is

 

 

no longer valid once the dialog has

 

 

been closed.

 

 

 

 

7

Back in the MainForm.cs code

protected override void

 

window, override the OnActivated

OnActivated(EventArgs e)

 

method.

{

 

 

 

 

 

8

If the pixel dialog does not exist,

// Update toggle button if required

 

then make sure our button is not

if (_dlgPixel == null

 

pushed down.

|| _dlgPixel.IsDisposed)

 

{

 

 

 

 

AssignPixelToggle(false);

 

 

}

 

 

 

9

Otherwise, set the button state

else

 

based on the Visible property of

AssignPixelToggle(_dlgPixel.Visible);

 

the pixel dialog.

base.OnActivated(e);

 

 

 

 

}

 

 

 

This code ensures that whenever the user closes the PixelDlg form, the main form is activated and the toggle toolbar button immediately updated. Compile and run the application to ensure that it works as expected.

TRY IT! Add two new menus to the top of the View menu called menuToolBar and menuStatusBar. Implement these menus to show and hide the corresponding controls in the application. Use the Visible property inherited from the Control class to identify the control’s current state and set it to the opposite one. If you are careful, you can implement a single Click

TOOLBAR BUTTONS

429

handler for both menus by using the sender parameter and observing that both objects are Control instances. When you run the program with these changes, note how the control shows or hides their contained buttons or panels as well.

This completes our discussion of toolbars. We now move on to the mostly unrelated but similarly named ToolTip class.

13.4TOOL TIPS

You never know when a good tip might come in handy. In Windows applications, tool tips provide short and quick explanations of the purpose of a control or other object. A number of classes provide their own tool tip mechanism through a ToolTipText property, in particular the StatusBarPanel, TabPage, and ToolBarButton classes. For classes derived from the Control object, the ToolTip class handles this logic in a general fashion.

.NET Table 13.5 ToolTip class

The ToolTip class is a component that provides a small popup window for a control. This window normally contains a short phrase describing the purpose of the control, and appears whenever the mouse hovers over the control for a configurable amount of time. This class is part of the System.Windows.Forms namespace, and supports the IExtenderProvider interface. The ToolTip class derives from the

System.ComponentModel.Component class.

 

Active

Gets or sets whether the ToolTip is currently active. When

 

 

false, no tool tips will appear. The default is true.

 

AutomaticDelay

Gets or sets the default delay time in milliseconds.

 

 

Whenever this property is set, the AutoPopDelay,

 

 

InitialDelay, and ReshowDelay properties are initialized.

 

 

The default is 500.

 

AutoPopDelay

Gets or sets the time in milliseconds before a displayed tool

 

 

tip will disappear. The default is ten times the

Public

 

AutomaticDelay setting.

 

 

Properties

InitialDelay

Gets or sets the time in milliseconds before a tool tip will

 

 

 

appear when the mouse is stationary. The default is the

 

 

AutomaticDelay setting.

 

ReshowDelay

Gets or sets the time in milliseconds after the first tool tip is

 

 

displayed before subsequent tool tips are displayed as the

 

 

mouse moves from one assigned control to another. The

 

 

default is one-fifth (1/5) the AutomaticDelay setting.

 

ShowAlways

Gets or sets whether to display the tool tip for an inactive

 

 

control. The default is false.

 

 

 

 

GetToolTip

Retrieves the tool tip string associated with a given control.

Public

RemoveAll

Removes all tool tip strings defined in this component.

Methods

 

 

 

SetToolTip

Associates a tool tip string with a given control.

 

 

 

430

CHAPTER 13 TOOLBARS AND TIPS

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