chap005 (1) dialogue box menus presentattion

arulrajvetias 21 views 34 slides Sep 10, 2024
Slide 1
Slide 1 of 34
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34

About This Presentation

vb


Slide Content

Chapter 5Chapter 5
Menus, Common Dialog Menus, Common Dialog
Boxes, Sub Procedures Boxes, Sub Procedures
andand
Function ProceduresFunction Procedures
Programming InProgramming In
Visual Basic .NETVisual Basic .NET

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

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 3
MenusMenus
•Menu BarMenu Bar
–Contains menus which drop down to display list of Contains menus which drop down to display list of
menu itemsmenu items
•Each item has a name and text propertyEach item has a name and text property
•Each item has a click eventEach item has a click event
•Add MainMenu control to formAdd MainMenu control to form
–Appears in the Component Tray, below form, where Appears in the Component Tray, below form, where
nondisplay controls are shownnondisplay controls are shown
–Words "Words "Type HereType Here" appear at the top of the form" appear at the top of the form

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

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 5
Defining MenusDefining Menus
•To create the menus simply type where the words "To create the menus simply type where the words "Type Type
HereHere" appear at the top of the form" appear at the top of the form
•Include & symbol as you type to indicate keyboard access Include & symbol as you type to indicate keyboard access
keyskeys
•You are actually entering the Text property for a You are actually entering the Text property for a
MenuItem objectMenuItem object
•Change MenuItem object names in the Properties window Change MenuItem object names in the Properties window
and follow consistent naming conventionsand follow consistent naming conventions

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 6
Submenus Submenus
•Pop up to the right of a menu itemPop up to the right of a menu item
•Filled triangle to the right of the menu item indicates to the Filled triangle to the right of the menu item indicates to the
user the existence of a submenuuser the existence of a submenu
•Create submenus by moving to the right of a menu item Create submenus by moving to the right of a menu item
and typing the next item's textand typing the next item's text

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

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 8
Separator BarsSeparator Bars
•Used for grouping menu items according to their purposeUsed for grouping menu items according to their purpose
•Visually represented as a bar across the menuVisually represented as a bar across the menu
•Create using one of two methodsCreate using one of two methods
–Typing a single hyphen for the textTyping a single hyphen for the text
–Right-click on Menu Designer where you want a Right-click on Menu Designer where you want a
separator bar and choose Insert Separatorseparator bar and choose Insert Separator

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

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 10
Modifying Menu ItemsModifying Menu Items
•Right-click the Menu Bar to:Right-click the Menu Bar to:
–DeleteDelete
–Insert NewInsert New
–Insert SeparatorInsert Separator
–Edit NamesEdit Names
•Displays menu item Name property rather than Text Displays menu item Name property rather than Text
property on formproperty on form
•Drag and Drop menu items to new locationsDrag and Drop menu items to new locations

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 11
Menu PropertiesMenu Properties
•Enabled property, True/FalseEnabled property, True/False
•Checked property, True/FalseChecked property, True/False
–Used to indicate current state of menu item that can be Used to indicate current state of menu item that can be
turned on and offturned on and off
•Create keyboard shortcutsCreate keyboard shortcuts
–In Properties window for menu item, select the Shortcut In Properties window for menu item, select the Shortcut
propertyproperty
–Make choice from drop-down listMake choice from drop-down list

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 12
Standards for Windows MenusStandards for Windows Menus
•Follow Windows standards for applicationsFollow Windows standards for applications
•Include keyboard access keysInclude keyboard access keys
•Use standards for shortcut keys, if usedUse standards for shortcut keys, if used
•Place File menu at left end of menu bar and end File menu Place File menu at left end of menu bar and end File menu
with Exitwith Exit
•Help, if included, is placed at right end of menu barHelp, if included, is placed at right end of menu bar
FFile ile EEdit dit VView Formaiew Formatt HHelpelp

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 13
Common Dialog BoxesCommon Dialog Boxes
•Predefined standard dialog boxes for:Predefined standard dialog boxes for:
–File Opening and SavingFile Opening and Saving
–Font and Color selectionFont and Color selection
–Printing and PreviewingPrinting and Previewing
•Add appropriate Common Dialog control to formAdd appropriate Common Dialog control to form
–Appears in the Component TrayAppears in the Component Tray

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 14
Common Dialog ControlsCommon Dialog Controls
•OpenFileDialogOpenFileDialog
•SaveFileDialogSaveFileDialog
•FontDialogFontDialog
•ColorDialogColorDialog
•PrintDialogPrintDialog
•PrintPreviewDialogPrintPreviewDialog

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

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 16
Displaying a Windows Common Dialog Displaying a Windows Common Dialog
BoxBox
•Use ShowDialog method to display the common dialog Use ShowDialog method to display the common dialog
box at run timebox at run time
•ShowDialog only displays the dialog, it does not do ShowDialog only displays the dialog, it does not do
anything elseanything else
ColorDialog1.ShowDialog( )ColorDialog1.ShowDialog( )
FontDialog1.ShowDialog( )FontDialog1.ShowDialog( )

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

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 18
Using the Information from the Dialog Using the Information from the Dialog
BoxBox
•Code must be written to retrieve and use the choice made Code must be written to retrieve and use the choice made
by the user in the common dialog boxby the user in the common dialog box
•ExampleExample
–Color Dialog displayedColor Dialog displayed
–User selects color and clicks OKUser selects color and clicks OK
–Code must be written to apply the selected color to the Code must be written to apply the selected color to the
object(s)object(s)
titleLabel.BackColor = ColorDialog1.Color

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

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 20
Setting Initial ValuesSetting Initial Values
•Before executing the ShowDialog method, assign the Before executing the ShowDialog method, assign the
existing values of the object's properties that will be existing values of the object's properties that will be
alteredaltered
•When the dialog box appears, the current values will be When the dialog box appears, the current values will be
selectedselected
•If the user presses Cancel, property settings for the objects If the user presses Cancel, property settings for the objects
will remain unchangedwill remain unchanged
FontDialog1.Font = subTotalLabel.Font FontDialog1.Font = subTotalLabel.Font or or
ColorDialog1.Color = Me.BackColorColorDialog1.Color = Me.BackColor

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 21
Creating Context MenusCreating Context Menus
•Shortcut menus that pop up when you right-clickShortcut menus that pop up when you right-click
•Are specific to the component to which user is pointing, Are specific to the component to which user is pointing,
reflecting options available for that component or situationreflecting options available for that component or situation
•A context menu does not have a top level menuA context menu does not have a top level menu
•Application can have multiple context menusApplication can have multiple context menus

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 22
Creating Context Menus (continued)Creating Context Menus (continued)
•Add ContextMenu component to the formAdd ContextMenu component to the form
–Appears in the Component TrayAppears in the Component Tray
•Click on the words "Click on the words "Context MenuContext Menu", at the top of the form, ", at the top of the form,
the words "the words "Type HereType Here" appear" appear
•Click on the words "Click on the words "Type HereType Here" and proceed as you did for " and proceed as you did for
the main menuthe main menu

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 23
Connecting the Context Menu to a Form Connecting the Context Menu to a Form
or Objector Object
•Set the form or object’s ContextMenu property to the Set the form or object’s ContextMenu property to the
name of the ContextMenuname of the ContextMenu
•If there is more than one context menu defined, If there is more than one context menu defined,
choose from the listchoose from the list
•Add to the Handles clause of a MainMenu item to use Add to the Handles clause of a MainMenu item to use
the same procedure for a related ContextMenu itemthe same procedure for a related ContextMenu item

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 24
Writing General ProceduresWriting General Procedures
•A general procedure is reusable code which can be called A general procedure is reusable code which can be called
from multiple proceduresfrom multiple procedures
•Useful for breaking down large sections of code into Useful for breaking down large sections of code into
smaller unitssmaller units
•Two typesTwo types
–Sub Procedure performs actionsSub Procedure performs actions
–Function performs actions AND returns a value (the Function performs actions AND returns a value (the
return valuereturn value))

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 25
Creating a New Sub ProcedureCreating a New Sub Procedure
•In the Editor window enclose the lines of code with a set of In the Editor window enclose the lines of code with a set of
Private Sub and End Sub statementsPrivate Sub and End Sub statements
•To use the Sub Procedure, call it from another procedureTo use the Sub Procedure, call it from another procedure
•Code in a Sub Procedure cannot be executed unless called Code in a Sub Procedure cannot be executed unless called
from another procedurefrom another procedure
Private Sub ProcedureName( )
' Statements in the procedure.
End Sub

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 26
Passing Arguments to Procedures Passing Arguments to Procedures
(page 205)(page 205)
•Declare variable as local and pass to any called proceduresDeclare variable as local and pass to any called procedures
•If a sub procedure names an argument, any call to the procedure If a sub procedure names an argument, any call to the procedure
must supply the argumentmust supply the argument
•Name of local variable does not need to match name in sub Name of local variable does not need to match name in sub
procedure argument listprocedure argument list
•Number of arguments, sequence and data type must matchNumber of arguments, sequence and data type must match

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 27
Passing Arguments ByVal or ByRefPassing Arguments ByVal or ByRef
•ByVal (default)ByVal (default)
–Sends a copy, original cannot be alteredSends a copy, original cannot be altered
•ByRefByRef
–Sends a reference to the memory location where the Sends a reference to the memory location where the
original is stored and therefore the original can be original is stored and therefore the original can be
alteredaltered
•Examples page 206Examples page 206

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

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 29
Writing Function ProceduresWriting Function Procedures
•In the Editor window enclose the linesIn the Editor window enclose the lines
of code with Private Function( ) and End Function of code with Private Function( ) and End Function
statementsstatements
•To use the Function, Call it by using it in an expressionTo use the Function, Call it by using it in an expression
•Pass arguments ByVal or ByRefPass arguments ByVal or ByRef
Private Function FunctionName( ) As Datatype
' Statements to execute.
End Function

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 30
Returning the Result of a FunctionReturning the Result of a Function
•To return a value to the calling procedure set up a return To return a value to the calling procedure set up a return
valuevalue
•The return value will be placed by VB in a variable with The return value will be placed by VB in a variable with
the SAME name as the Function's namethe SAME name as the Function's name
OROR
•Use the Return statement to return the valueUse the Return statement to return the value

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

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.5- 32
Functions with Multiple ArgumentsFunctions with Multiple Arguments
•Functions can receive one or more arguments (values)Functions can receive one or more arguments (values)
•Sequence and data type of arguments in Call must exactly match Sequence and data type of arguments in Call must exactly match
arguments in function headerarguments 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.5- 33
Breaking Calculations into Smaller Breaking Calculations into Smaller
UnitsUnits
•Projects with many calculations are easier to understand Projects with many calculations are easier to understand
and write if calculations are broken into small unitsand write if calculations are broken into small units
•Each unit should perform one program function or logic Each unit should perform one program function or logic
blockblock

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