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

Set the version number of the MyPhotos application to 13.3.

ADD THE TOOLBAR BUTTONS FOR THE FILE MENU

 

 

 

 

Action

 

 

 

Result

 

 

 

 

 

 

 

 

 

1

 

In the MainForm.cs [Design]

The images in our image list are now available to any

 

 

window, modify the properties

buttons placed on the toolbar.

 

 

for the toolBarMain control.

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

ButtonSize

 

16, 16

 

 

 

 

 

ImageList

 

imageListToolBar

 

 

 

 

 

 

 

 

 

 

 

2

 

Display the ToolBarButton

 

 

 

Collection Editor window.

 

 

 

How-to

 

 

 

 

 

 

 

 

In the toolbar’s Properties

 

 

 

window, click the button

 

 

 

associated with the Buttons

 

 

 

item.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3

 

Click the Add button three times

 

 

 

to create three new

 

 

 

 

 

 

ToolBarButton objects.

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

Button

 

Property

Value

 

 

 

0 (New)

 

(Name)

tbbNew

 

 

 

 

ImageIndex

0

 

 

 

 

 

 

ToolTipText

Create

Note: You can modify the properties for these

 

 

 

 

 

 

album

objects in the collection editor or in the Properties

 

 

1 (Open)

 

(Name)

tbbOpen

 

 

 

window. The collection editor is shown here. To use

 

 

 

ImageIndex

1

 

 

the Properties window, create the buttons, click OK

 

 

 

ToolTipText

Open

to close the window, and then simply select the

 

 

 

 

 

 

album

desired toolbar button from the list at the top of the

 

 

2 (Save)

 

(Name)

tbbSave

Properties window.

 

 

 

ImageIndex

2

 

 

 

 

 

 

ToolTipText

Save

 

 

 

 

 

 

 

album

 

 

 

 

 

 

 

 

 

 

 

4

 

Click OK to close the editor.

The new buttons appear on the form.

 

 

 

 

 

 

 

 

 

 

Our ToolBar now contains three toolbar buttons. Visual Studio displays the images associated with each button in the designer window. If text is assigned to a button,

TOOLBAR BUTTONS

421

Visual Studio will display this as well, assuming the button provides room for the text to appear.

The next step is to link these to operations within our form. The ButtonClick event in the ToolBar class is used for this purpose. Event handlers for this event receive a ToolBarButtonClickEventArgs parameter that contains a Button property. This property retrieves the ToolBarButton instance clicked by the user.

One means for handling our button clicks uses a series of if statements. The code would look something like the following:

private void toolBarMain_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)

{

// Determine which button was clicked – not our approach if (e.Button == tbbNew)

{

menuNew.PerformClick();

}

else if (e.Button == tbbOpen)

{

menuOpen.PerformClick();

}

else if (e.Button == tbbSave)

{

menuSave.PerformClick();

}

}

This can get a bit unwieldy as the number of buttons increase. A more elegant approach takes advantage of the Tag property for ToolBarButton instances. This property holds an object instance, and in particular can hold a MenuItem object to associate with the button. Since we would like our buttons to perform the same action as the corresponding menu item, we will associate the proper menu item with each of our buttons. Continuing our prior steps:

SET THE TAG PROPERTY FOR THE TOOLBAR BUTTONS

 

Action

Result

 

 

 

5

Create a new InitToolBarButtons

private void InitToolBarButtons()

 

method in the MainForm.cs code window.

{

 

 

 

6

Set the Tag property for each toolbar

tbbNew.Tag = menuNew;

 

button to the corresponding MenuItem

tbbOpen.Tag = menuOpen;

 

object.

tbbSave.Tag = menuSave;

 

}

 

 

 

 

 

7

Add a call to this new method from the

public MainForm()

 

MainForm instance constructor.

{

 

 

. . .

 

 

InitToolBarButtons();

 

 

}

 

 

 

422

CHAPTER 13 TOOLBARS AND TIPS

Our implementation of the ButtonClick handler for our toolbar can now take advantage of these settings to simply invoke the Click event handler associated with the corresponding menu item.

HANDLE THE BUTTONCLICK EVENT FOR THE TOOLBAR

 

Action

Result

 

 

 

8

Add a ButtonClick event handler for

private void toolBarMain_ButtonClick

 

the ToolBar control.

(object sender, System.Windows.Forms.

 

How-to

ToolBarButtonClickEventArgs e)

 

{

 

This is the default event for toolbars, so

 

 

simply double-click the toolbar control

 

 

in the MainForm.cs [Design] window.

 

 

 

 

9

Implement this handler using the Tag

// Handle menu buttons

 

property of the ToolBarButton

MenuItem mi = e.Button.Tag as MenuItem;

 

component.

if (mi != null)

 

mi.PerformClick();

 

 

 

 

}

 

 

 

Note how the as keyword is used to ensure that the Tag property does, in fact, refer to a MenuItem object. If a new button is added without an associated menu, then this code is safely ignored.

We can also use this method for the Next and Previous toolbar buttons. The following steps also define a separator button to differentiate between these two sets of buttons.

ADD THE NEXT AND PREVIOUS TOOLBAR BUTTONS

 

 

 

Action

 

 

Result

 

 

 

 

 

 

 

10

In the ToolBarButton Collection Editor,

 

 

add three new toolbar buttons.

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

Button

Property

Value

 

 

3

Style

Separator

 

 

 

4 (Prev)

(Name)

tbbPrevious

 

 

 

 

ImageIndex

3

 

 

 

 

 

ToolTipText

Previous

 

 

 

 

 

image

 

 

 

5 (Next)

(Name)

tbbNext

 

 

 

 

ImageIndex

4

 

 

 

 

 

ToolTipText

Next image

 

 

 

 

 

 

 

 

11

Update the InitToolBarButtons

private void InitToolBarButtons()

 

method for these new buttons.

{

 

 

 

 

 

 

tbbNew.Tag = menuNew;

 

 

 

 

 

 

tbbOpen.Tag = menuOpen;

 

 

 

 

 

 

tbbSave.Tag = menuSave;

 

 

 

 

 

 

tbbPrevious.Tag = menuPrevious;

 

 

 

 

 

 

tbbNext.Tag = menuNext;

 

 

 

 

 

 

}

 

 

 

 

 

 

 

TOOLBAR BUTTONS

423

Our ButtonClick event handler automatically handles these buttons based on their associated menu items, so no further changes are needed. Compile and run the program to make use of these buttons. Note how the tool tips pop up when the mouse hovers over these buttons.

TRY IT! Two things to try here. First, modify the Appearance property for the toolbar to be Flat. The buttons will no longer have a three-dimensional appearance, and the separator will be a line between the two sets of buttons.

Second, modify the Dock property for the toolbar to be Left. This places the control on the left side of the form. Run the program to verify that everything still works as expected.

There are two other types of toolbar buttons, namely the DropDownButton and ToggleButton styles. The next two sections take a look at these alternate button styles.

13.3.2ADDING A DROPDOWN BUTTON

To create a dropdown menu on our form, we will make use of our existing Images submenu displayed via the menuImages menu item created way back in chapter 3, and updated in chapter 6. The changes are detailed by the following steps, and discussed in the subsequent text.

ADD A DROPDOWN BUTTON

 

Action

Result

 

 

 

1

In the MainForm.cs [Design] window,

A second context menu appears in the

 

add a new ContextMenu object to the

component tray.

 

MainForm window.

 

 

 

 

2

Set the (Name) for the menu to

 

 

ctxtMenuImage.

 

 

 

 

3

Assign the menuImage_Popup event

Note: If you look at our implementation of

 

handler as the Popup event handler for

this event handler back in chapter 3, you

 

the ctxtMenuImage menu.

will find that we cast the sender parame-

 

 

ter to a Menu object, rather than a Menu-

 

 

Item object, so that it would work with any

 

 

type of menu.

 

 

 

4

Modify the DefineContextMenu

private void DefineContextMenu()

 

method to copy the contents of the

{

 

menuImage menu into the new context

//Copy View menu into ctxtMenuView

 

. . .

 

menu.

 

// Copy Image menu into ctxtMenuImage

 

Note: This clones the submenus of the

foreach (MenuItem mi in

 

menuImage.MenuItems)

 

menuImage object and assigns them to

 

{

 

the ctxtMenuImage object. We created

ctxtMenuImage.MenuItems.

 

and discussed this method in chapter 3.

Add(mi.Index, mi.CloneMenu());

 

 

}

 

 

}

 

 

 

424

CHAPTER 13 TOOLBARS AND TIPS

ADD A DROPDOWN BUTTON (continued)

 

 

 

Action

 

 

Result

 

 

 

 

 

 

 

5

 

In the ToolBarButton Collection Editor,

 

 

 

add two new toolbar buttons.

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

Button

Property

Value

 

 

6

Style

Separator

 

 

 

 

(Name)

tbbImage

Note: The down arrow to the right of the

 

 

 

DropDownMenu

ctxtMenuImage

image appears because the toolbar’s

 

 

7 (Image)

ImageIndex

5

 

DropDownArrow property is true. Set this

 

 

 

Style

DropDownButton

property to false to display the button

 

 

 

without the arrow.

 

 

 

ToolTipText

Set display mode

 

 

 

 

 

 

 

 

 

 

 

Our new toolbar button requires a new context menu, which we use as the dropdown menu for our new button. Even though the DropDownMenu property for the ToolBarButton class is defined as a type of Menu object, a ContextMenu instance is required to properly display a dropdown menu beneath the button. We could have used the ctxtMenuView context menu, although we would then display the entire View menu beneath the toolbar button.

Compile, run, open, click, and otherwise make sure the new button works. The

.NET Framework does all the hard work here. When the button is clicked, the menu item collection associated with the ctxtMenuImage menu is displayed. This causes the Popup event associated with this menu to fire, invoking the menuImage_Popup event handler. Figure 13.2 shows the application with the popup menu displayed for our new button.

Figure 13.2

When the down arrow for a toolbar button is shown, as it is here, the user must click on this arrow to display the associated menu.

TOOLBAR BUTTONS

425

ToolBarButtonClickEvent-Args

Of course, you don’t always have an existing menu in your menu bar to clone and use in your dropdown toolbar buttons. A custom ContextMenu object can be created and assigned to the button. If desired, you can also draw your own menu items similar to how we drew a custom status bar panel in chapter 4 and list box items in chapter 10.

Alternately, you may wish to forgo a menu entirely and create a custom window to associate with your button. This can be done as well. The ButtonDropDown event occurs for the parent ToolBar control whenever a dropdown menu on the control is clicked. Event handlers for this event receive a

class instance as the event parameter. This class contains a Button property to retrieve the dropdown ToolBarButton instance that was clicked. The Rectangle property for the button identifies the location of the button and can be used to properly place a small window or other graphical object at the proper location.

Our last style of button is a toggle button.

13.3.3ADDING A TOGGLE BUTTON

Our final toolbar button will illustrate the ToggleButton style to show and hide the pixel data dialog created in chapter 8. Toggle buttons, as you may recall from chapter 9, provide two different states: one when the button is pressed in, and one when it is not.

We will do something a little different here that seems appropriate for our example. If you are keeping track of the images available in our image list, you may realize there are two images left, while only one more button. We will use one image when the button is pushed in, and the other when it is not. Figure 13.3 shows our application with the button pressed in and the PixelDlg form displayed.

Figure 13.3

The toggle button on our toolbar displays a green light when the pixel dialog is shown, and a red light otherwise.

426

CHAPTER 13 TOOLBARS AND TIPS

This will require some coordination with the rest of the application to make sure the button is never pressed when the pixel dialog is hidden. We’ll begin by creating the button and implementing the ButtonClick event support.

 

 

 

 

 

ADD THE TOGGLE TOOLBAR BUTTONS

 

 

 

 

 

 

 

 

 

 

 

ACTION

 

 

RESULT

 

 

 

 

 

 

 

1

In the ToolBarButton Collection Editor,

 

 

add two new toolbar buttons.

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

Button

Property

 

Value

 

 

8

Style

 

Separator

 

 

 

 

(Name)

 

tbbPixelData

 

 

 

9 (Pixel)

ImageIndex

 

6

 

 

 

 

Style

 

ToggleButton

 

 

 

 

 

 

 

 

 

ToolTipText

 

Show pixel data

 

 

 

 

 

 

 

 

 

2

Implement an AssignPixelData

protected void

 

method in the MainForm.cs code

AssignPixelToggle(bool push)

 

window to adjust the button settings

{

 

tbbPixelData.Pushed = push;

 

based a specified value.

 

 

 

 

 

if (push)

 

How-to

 

 

 

 

{

 

 

 

 

 

tbbPixelData.ImageIndex = 7;

 

Display the green light icon when the

 

tbbPixelData.ToolTipText

 

button is pushed, and the red light

= "Hide pixel data";

 

otherwise.

 

 

 

 

}

 

Note: This will be used by various meth-

else

 

{

 

ods to update the toggle button as the

tbbPixelData.ImageIndex = 6;

 

state of the pixel data dialog changes.

tbbPixelData.ToolTipText

 

= "Show pixel data";

 

 

 

 

 

 

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

3

Update the ButtonClick event handler

private void toolBarMain_ButtonClick

 

to adjust the state of both the dialog and

(object sender,

 

the button when the toggle is clicked.

ToolBarButtonClickEventArgs e)

 

{

 

 

 

 

 

 

 

 

How-to

 

 

 

 

// Handle menu buttons

 

 

 

 

 

. . .

 

a. When the button is pushed, invoke

 

// Handle Pixel Data button

 

 

the Pixel Data menu to ensure the

if (e.Button == tbbPixelData)

 

 

dialog is displayed.

 

 

{

 

b. Otherwise, hide the dialog if it is cur-

if (e.Button.Pushed)

 

{

 

 

rently displayed.

 

 

// Display pixel dialog

 

c. Also call the AssignPixelToggle

menuPixelData.PerformClick();

 

}

 

 

method to update the button settings.

 

 

else if (this._dlgPixel != null

 

 

 

 

 

 

 

&& _dlgPixel.Visible)

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

// Hide pixel dialog

 

 

 

 

 

 

 

_dlgPixel.Hide();

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

// Update the button settings

 

 

 

 

 

 

 

AssignPixelToggle(e.Button.Pushed);

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

TOOLBAR BUTTONS

427

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