Chapter 5 Menus, Common Dialog Boxes, Sub Procedures and Function Procedures Programming In Visual Basic .NET Menu <ALT><F> Menu Items 5- 2 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Menus • Menu Bar – Contains menus which drop down to display list of menu items • Each item has a name and text property • Each item has a click event • Add MainMenu control to form – Appears in the Component Tray, below form, where nondisplay controls are shown – Words "Type Here" appear at the top of the form 5- 3 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Menus (continued) Type first Menu here MainMenu Control appears in Component Tray 5- 4 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Defining Menus • To create the menus simply type where the words "Type Here" appear at the top of the form • Include & symbol as you type to indicate keyboard access keys • You are actually entering the Text property for a MenuItem object • Change MenuItem object names in the Properties window and follow consistent naming conventions 5- 5 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Submenus • Pop up to the right of a menu item • Filled triangle to the right of the menu item indicates to the user the existence of a submenu • Create submenus by moving to the right of a menu item and typing the next item's text 5- 6 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Submenus (continued) 5- 7 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Separator Bars • Used for grouping menu items according to their purpose • Visually represented as a bar across the menu • Create using one of two methods – Typing a single hyphen for the text – Right-click on Menu Designer where you want a separator bar and choose Insert Separator 5- 8 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Coding for Menu Items • Double-click any menu item to open the item’s click event procedure • Write code for the click event procedure 5- 9 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Modifying Menu Items • Right-click the Menu Bar to: – Delete – Insert New – Insert Separator – Edit Names • Displays menu item Name property rather than Text property on form • Drag and Drop menu items to new locations 5- 10 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Menu Properties • Enabled property, True/False • Checked property, True/False – Used to indicate current state of menu item that can be turned on and off • Create keyboard shortcuts – In Properties window for menu item, select the Shortcut property – Make choice from drop-down list 5- 11 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Standards for Windows Menus • • • • Follow Windows standards for applications Include keyboard access keys Use standards for shortcut keys, if used Place File menu at left end of menu bar and end File menu with Exit • Help, if included, is placed at right end of menu bar File Edit View Format Help 5- 12 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Common Dialog Boxes • Predefined standard dialog boxes for: – File Opening and Saving – Font and Color selection – Printing and Previewing • Add appropriate Common Dialog control to form – Appears in the Component Tray 5- 13 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Common Dialog Controls • • • • • • 5- 14 OpenFileDialog SaveFileDialog FontDialog ColorDialog PrintDialog PrintPreviewDialog © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Common dialog controls Component tray 5- 15 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Displaying a Windows Common Dialog Box • Use ShowDialog method to display the common dialog box at run time • ShowDialog only displays the dialog, it does not do anything else ColorDialog1.ShowDialog( ) FontDialog1.ShowDialog( ) 5- 16 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. With OpenFileDialog1 .ShowDialog() End With Code 5- 17 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Using the Information from the Dialog Box • Code must be written to retrieve and use the choice made by the user in the common dialog box • Example – Color Dialog displayed – User selects color and clicks OK – Code must be written to apply the selected color to the object(s) titleLabel.BackColor = ColorDialog1.Color 5- 18 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Color and Font Dialogs 5- 19 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Setting Initial Values • 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 FontDialog1.Font = subTotalLabel.Font or ColorDialog1.Color = Me.BackColor 5- 20 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Creating Context Menus • Shortcut menus that pop up when you right-click • Are specific to the component to which user is pointing, reflecting options available for that component or situation • A context menu does not have a top level menu • Application can have multiple context menus 5- 21 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Creating Context Menus (continued) • Add ContextMenu component to the form – Appears in the Component Tray • Click on the words "Context Menu", at the top of the form, the words "Type Here" appear • Click on the words "Type Here" and proceed as you did for the main menu 5- 22 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Connecting the Context Menu to a Form or Object • Set the form or object’s ContextMenu property to the name of the ContextMenu • If there is more than one context menu defined, choose from the list • Add to the Handles clause of a MainMenu item to use the same procedure for a related ContextMenu item 5- 23 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Writing General Procedures • A general procedure is reusable code which can be called from multiple procedures • Useful for breaking down large sections of code into smaller units • Two types – Sub Procedure performs actions – Function performs actions AND returns a value (the return value) 5- 24 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Creating a New Sub Procedure • In the Editor window enclose the lines of code with a set of Private 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 5- 25 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Passing Arguments to Procedures (page 205) • Declare variable as local and pass to any called procedures • If a sub procedure names an argument, any call to the procedure must supply the argument • Name of local variable does not need to match name in sub procedure argument list • Number of arguments, sequence and data type must match 5- 26 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Passing Arguments ByVal or ByRef • ByVal (default) – Sends a copy, original cannot be altered • ByRef – Sends a reference to the memory location where the original is stored and therefore the original can be altered • Examples page 206 5- 27 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Sub Procedure Example Private Sub SelectColor(incomingColor As Color) With ColorDialog1 .Color = incomingColor Sub Procedure .ShowDialog( ) End With End Sub Private Sub changeTitleButtonColor_Click( ) Dim originalColor As Color originalColor = titleLabel.ForeColor SelectColor(originalColor) titleLabel.ForeColor = ColorDialog1.Color End Sub 5- 28 Calling Procedure © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Writing Function Procedures • In the Editor window enclose the lines of code with Private Function( ) and End Function statements • To use the Function, Call it by using it in an expression • Pass arguments ByVal or ByRef Private Function FunctionName( ) As Datatype ' Statements to execute. End Function 5- 29 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Returning the Result of a Function • 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 5- 30 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Function Example Private Function Commission(ByVal salesAmountDecimal As Decimal) _ As Decimal If salesAmountDecimal < 100D Then Commission = 0D Else Commission = 0.15 * salesAmountDecimal End If End Function Function Private Sub calculateButton_Click( ) Calling Dim salesDecimal As Decimal Procedure salesDecimal = Decimal.Parse(salesTextBox.Text) commissionLabel.Text = Commission(salesDecimal.ToString("C")) End Sub 5- 31 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Functions with Multiple Arguments • Functions can receive one or more arguments (values) • Sequence and data type of arguments in Call must exactly match arguments in function header Private Function Payment(ByVal rateDecimal As Decimal, _ ByVal timeDecimal As Decimal, ByVal amountDecimal _ As Decimal) As Decimal paymentLabel.Text = Payment(Decimal.Parse(rateTextBox.Text), _ Decimal.Parse(yearsTextBox.Text), _ Decimal.Parse(principalTextBox.Text)).ToString( ) End Function 5- 32 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Breaking Calculations into Smaller Units • Projects with many calculations are easier to understand and write if calculations are broken into small units • Each unit should perform one program function or logic block 5- 33 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 34 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.