Professional Documents
Culture Documents
McGraw-Hill
Objectives
Create menus and submenus for program control. Display and use the Windows common dialog boxes. Create context menus for controls and the form. Write reusable code in sub procedures and function procedures and call the procedures from other locations.
McGraw-Hill
5-2
Menus
Menu Bar
Easy to create menus for a Windows form using the Visual Studio environments Menu Designer Menus will look and behave like standard Windows menus.
2009 The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill
5-3
Defining Menus (1 of 2)
MenuStrip component is added to a form. MenuStrip is a container to which ToolStripMenuItems, ToolStripComboBoxes, ToolStripSeparators, and ToolStripTextBoxes can be added.
2009 The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill
5-4
Defining Menus (2 of 2)
The MenuStrip component appears in the component tray below the form and the Menu Designer allows you to begin typing the text for the menu items.
McGraw-Hill
5-5
Holds the words that appear on the screen like the Text property of a button or label To conform to Windows standards, the first menus Text property should be File, with a keyboard access key. Use the ampersand (&) in the text to specify the key to use for keyboard access. Enter and change the Text property for each menu and menu item using the Menu Designer or make the changes in the Text property using the Properties window.
2009 The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill
5-6
The File menu item that is added is automatically named FileToolStripMenuItem. The items are named so well that there wont be a need to change the Name property of any menu component. If the Text property is changed for any menu item, the item is not automatically renamed; it will need to be renamed.
McGraw-Hill
5-7
ToolStripMenu Items in the collection can be displayed, reordered, added, and deleted using the Items Collection Editor.
McGraw-Hill
5-8
Submenus
A filled triangle to the right of the menu item indicates the existence of a submenu. Create submenus by moving to the right of a menu item and typing the next item's text.
2009 The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill
5-9
Separator Bars
Used for grouping menu items according to their purpose Visually represented as a bar across the menu To create a separator bar, add a new menu item and click on its drop-down arrow.
2009 The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill
5-10
Menu Properties
Checked property, False/True can be set at design or run time Used to indicate that an option is selected Setting keyboard shortcuts
McGraw-Hill
5-11
Follow Windows standards for applications. Include keyboard access keys. Use standards for shortcut keys, if used. Place the File menu at left end of menu bar and end File menu with the Exit command. Help, if included, is placed at right end of menu bar. File Edit View Format Help
McGraw-Hill
5-12
Add appropriate Common Dialog components to display the dialog boxes that are provided as part of the Windows environment.
McGraw-Hill
5-13
5-14
Use ShowDialog method to display the common dialog box at run time. ShowDialog only displays the dialog.
ColorDialog1.ShowDialog( ) FontDialog1.ShowDialog( )
McGraw-Hill
5-15
A dialog box is said to be modal when it stays on top of the application and must be responded to.
it is a window displayed modally.
McGraw-Hill
5-16
Code must be written to retrieve and use the choice made by the user in the common dialog box. Example
Color Dialog is displayed. User selects color and clicks OK the selected
color is stored in a property that can be accessed. Color that is selected is stored in the Color property and can be assigned to another object such as a control.
titleLabel.BackColor = .ColorDialog1.Color
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
5-17
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 selected. If the user presses Cancel, property settings for the objects will remain unchanged.
5-18
Shortcut menus 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. Application can have multiple context menus.
2009 The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill
5-19
A general procedure is reusable code that can be called from multiple procedures. Useful for breaking down large sections of code into smaller units Two types
value).
Sub Procedure performs actions. Function performs actions AND returns a value (the return
McGraw-Hill
5-20
Declare variable as local and pass to any called procedures (can be module level, but it makes the variable visible to all other procedures) If a sub procedure names an argument, any call to the procedure must supply the argument. Name of the argument does not have to be the same in both locations. Number of arguments, sequence, and data type must match.
2009 The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill
5-21
In the Editor window, enclose the lines of code with a set of Sub and End Sub statements. To use the Sub Procedure, call it from another procedure. Code in a Sub Procedure cannot be executed unless called from another procedure.
Private Sub ProcedureName( ) ' Statements in the procedure. End Sub
McGraw-Hill
5-22
Procedure
Private Sub ChangeTitleButton_Click( ) Dim originalColor As Color originalColor = TitleLabel.ForeColor SelectColor(originalColor) TitleLabel.ForeColor = ColorDialog1.Color End Sub
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
Calling Procedure
5-23
ByVal value Sends a copy of the arguments value; original cannot be altered. ByRef reference Sends a reference to the memory location where the original is stored and therefore the procedure may change the arguments value; original can be altered If not specified, arguments are passed by value.
2009 The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill
5-24
5-25
To return a value to the calling procedure, set up a return value. The return value will be placed by VB in a variable with the SAME name as the Function's name. --OR-Use the Return statement to return the value.
McGraw-Hill
5-26
Function Example
Private Function Commission(ByVal SalesAmountDecimal As Decimal) _ As Decimal If SalesAmountDecimal < 1000D Then Commission = 0D ElseIf SalesAmountDecimal <= 2000D Then Commission = 0.15D* SalesAmountDecimal Else Function Commission = 0.2D * SalesAmountDecimal End If End Function Private Sub CalculateButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles CalculateButton.Click Calling Dim SalesDecimal As Decimal Procedure SalesDecimal = Decimal.Parse(SalesTextBox.Text) CommissionLabel.Text = Commission(SalesDecimal).ToString("C") End With End Sub
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
5-27
5-28
McGraw-Hill
5-29