Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming In Visual Basic .NET

Similar presentations


Presentation on theme: "Programming In Visual Basic .NET"— Presentation transcript:

1 Programming In Visual Basic .NET
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures and Function Procedures Programming In Visual Basic .NET

2 <ALT><F>
Menu <ALT><F> Menu Items © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

3 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

4 Menus (continued) Type first Menu here
MainMenu Control appears in Component Tray © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

5 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

6 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

7 Submenus (continued) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

8 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

9 Coding for Menu Items Double-click any menu item to open the item’s click event procedure Write code for the click event procedure © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

10 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

11 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

12 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

13 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

14 Common Dialog Controls
OpenFileDialog SaveFileDialog FontDialog ColorDialog PrintDialog PrintPreviewDialog © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

15 Common dialog controls
Component tray © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

16 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( ) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

17 With OpenFileDialog1 .ShowDialog() End With Code
© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

18 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

19 Color and Font Dialogs © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

20 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

21 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

22 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

23 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

24 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) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

25 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

26 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

27 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

28 Sub Procedure Calling Procedure Sub Procedure Example
Private Sub SelectColor(incomingColor As Color) With ColorDialog1 .Color = incomingColor .ShowDialog( ) End With End Sub Sub Procedure Private Sub changeTitleButtonColor_Click( ) Dim originalColor As Color originalColor = titleLabel.ForeColor SelectColor(originalColor) titleLabel.ForeColor = ColorDialog1.Color End Sub Calling Procedure © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

29 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

30 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

31 Function Function Example Calling Procedure
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( ) Dim salesDecimal As Decimal salesDecimal = Decimal.Parse(salesTextBox.Text) commissionLabel.Text = Commission(salesDecimal.ToString("C")) End Sub Calling Procedure © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

32 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

33 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

34 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.


Download ppt "Programming In Visual Basic .NET"

Similar presentations


Ads by Google