Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in...

18
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls with properties and events. MenuStrip component is added to a form. It is a container to which ToolStripMenuItems, ToolStripComboBoxes, ToolStripSeparators, and ToolStripTextBoxes can be added.

description

The Text and Name Properties The Text property holds the words that appear on the screen — like the Text property of a button or label Use the ampersand ( & ) in the text to specify the key to use for keyboard access The menu item Name property that is added (e.g., File ) is automatically named FileToolStripMenuItem. The items are named so well that there usually isn’t a need to change the Name property of any menu component. Enter and change the two properties using the Menu Designer or make the changes using the Properties window.

Transcript of Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in...

Page 1: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Creating Menus

• Menu Bar – behaves like standard Windows menus

• Can be used in place of or in addition to buttons to execute a procedure

• Menu items are controls with properties and events.

• MenuStrip component is added to a form. It is a container to which ToolStripMenuItems, ToolStripComboBoxes, ToolStripSeparators, and ToolStripTextBoxes can be added.

Page 2: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

The Menu Designer

The MenuStrip componentappears in the component traybelow the form.

The Menu Designer allows you to begin typing the text for the menu items.

Page 3: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

The Text and Name Properties• The Text property holds the words that appear on the screen — like the Text

property of a button or label

• Use the ampersand (&) in the text to specify the key to use for keyboard access

• The menu item Name property that is added (e.g., File) is automatically named FileToolStripMenuItem.

• The items are named so well that there usually isn’t a need to change the Name property of any menu component.

• Enter and change the two properties using the Menu Designer or make the changes using the Properties window.

Page 4: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Two Other Important Menu Properties

•Enabled property: enables/ disables (greys out) a menu item. May be set to True or False.

•Checked property: places a check mark next to the menu item, indicated the menu item has been selected. May be set to True or False.

• Both properties can be set at design or run time.

Page 5: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Adding Submenus and Separator Bars

• To create submenus, move to the right of a menu item and typing the next item's text.

• To create a separator bar (horizontal line), add a new menu item and click on its drop-down arrow.

Page 6: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Editing Menu Designs

• The menu items in a menu design can be displayed, reordered, added, and deleted using the Items Collection Editor.

Page 7: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Follow the Standards for Windows Menus

• Include keyboard access keys.

• Place the File menu at left end of menu bar and end the File menu with the Exit command.

•Help, if included, is placed at right end of menu bar.

File Edit View Format Help

Page 8: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Adding Common Dialog Boxes

• Predefined standard dialog boxes for common Windows functions may be added to an application.

• Add the dialog box to the form, placing it in the component tray.

• Use the ShowDialog() method to display the common dialog box at run time:

ColorDialog1.ShowDialog()

Page 9: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Using the Information in the Dialog Box

• Code must be written to retrieve and use the choice made by the user in the dialog box.

• Example:• The Color Dialog is displayed.• User selects a color and clicks OK — the selected color is

stored in a property that can be accessed.• The color that is selected is stored in the Color property and

can be assigned to another object such as a control.

• Dialog boxes (for now) are modal meaning it stays on top of the application and must be responded to (i.e., user must click OK or Cancel in the box) before returning to the application.

TitleLabel.BackColor = ColorDialog1.Color

Page 10: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Setting Initial Values in a Dialog Box

• Before executing the ShowDialog() method, assign the existing values of the object's properties that will be altered.

• When the dialog box appears, the current values will be shown.

• If the user presses Cancel, property settings for the objects will remain unchanged.

ColorDialog1.Color = TitleLabel.BackColor

Page 11: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Creating Context Menus

• Context menus are shortcut that pop up when you right-click

• Items are specific to the component to which the user is pointing, reflecting options available for that component or situation.

• A ContextMenuStrip component is added and appears in the component tray below the form.

• A context menu does not have a top-level menu, only menu items.

Page 12: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Procedures• A procedure is reusable code that can be called from multiple places.

• Useful for breaking down large sections of code into smaller units.

• Two types of procedures:• A Sub procedure performs actions.• A Function procedure performs actions AND returns a value.

• If a procedure has argument(s), any call to the procedure must supply the argument(s).

• Name of the arguments don’t have to match but number of arguments, sequence, and data type must match.

Page 13: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Creating a Sub Procedure• Enclose lines of code with a set of Sub and End Sub statements.

• For now, make procedures Private in scope.

• Code in Sub procedure is executed when called from another procedure.

Private Sub ProcedureName( )' Statements in the procedure.

End Sub

' Statements in the another procedure. ProcedureName()

Page 14: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Sub Procedure Example

Private Sub SelectColor(incomingColor As Color)With ColorDialog1

.Color = incomingColor

.ShowDialog( )End With

End Sub

Private Sub ChangeTitleButton_Click( ) Dim OriginalColor As Color OriginalColor = TitleLabel.ForeColor SelectColor(originalColor) TitleLabel.ForeColor = ColorDialog1.ColorEnd Sub

This Sub Procedure is called below.

Page 15: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Creating a Function Procedure• Enclose the lines of code with Function and End Function statements. For now, make

functions Private in scope.

• The return value is placed in the Return statement, usually the last statement of the function.

• Since the procedure returns a value, a data type for the value must be specified.

• To use the Function, Call it by using it in an expression.

Private Function FunctionName() As Datatype ' Statements to execute. Return return valueEnd Function

' Statements in the another procedure. variable = FunctionName()

Page 16: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

Function Example

Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click Dim myArea, myRadius myRadius = Decimal.Parse(RadiusTextBox.Text) myArea = Area(myRadius) AreaLabel.Text = myArea.ToString(“N2”)End Sub

Private Function Area(ByVal Radius As Decimal) As Decimal Dim Area

If Radius > 0 Then Area = 3.14159D * Radius ^ 2 Else Area = 0 End If Return AreaEnd Function

This Function Procedure is called below.

Page 17: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

ByVal and ByRef• There are two ways arguments can be passed to a Sub or Function.

• ByVal — passed by value –a copy of the argument’s value is passed to the procedure;– the original argument cannot be altered.

• ByRef — passed by reference–a reference to the memory location where the original is stored is

passed to the procedure;– the original argument could possibly be altered

• If not specified, arguments are passed by value.

Page 18: Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.

A Complete Example (Exercise 5.5, p. 246)

Create a flag viewer applications whose user interface is a menu bar of options.

The user may choose which flag to display and the options that are displayed along with the flag.