Programming In Visual Basic.
NET
Lesson 4:
Menus, Sub Procedures and Sub Functions
Menus Control
• The Menus control allows developers to create and manage
menus in their applications.
• Menus provide a way for users to access various features
and commands in a structured and organized manner.
• The most common types of menus in VB.NET applications
are the
• MainMenu
• ContextMenu (or ContextMenuStrip).
Menus Control
• Menu Bar
• Drop-down list of commands
• Have properties
• Have events to write code for
• Add MainMenu control to form
• Appears in the Component Tray, pane at bottom of Form
Designer where nondisplay controls are shown
• Words "Type Here" appear at the top of the form
3
Menu control
• Click on Menus & Toolbars to
expand the list of controls
• Double click on the MenuStrip
to open the menu editor in
your form
• See next slide for sample
screen
4
Menu Designer Initially
Type first Menu here
MainMenu Control
appears in
Component Tray
5
Using the Menu Designer
• To create the menus • You are entering the Text
simply type where the property for a MenuItem
words "Type Here" object
• Change MenuItem object
appear at the top of the names in the Properties
form Window to include mnu
• Include & symbol as prefix
you type to indicate
Keyboard Access Keys.
Example &File
6
Submenus
•Popup to the right of the menu
•Filled triangle to the right of the menu item
indicates to the user the existence of a
submenu
•Avoid more than one level deep
•Create submenus by moving to the right of a
menu item and typing the next item's text
7
SubMenus (cont.)
8
Other options
Right click on the menu item Document Outline (Example)
Enable/Disable the menu
10
Other options
Menu Properties
•Text
•Name, prefix = mnu
• Examples: mnuFileExit, mnuHelpAbout,
mnuFormatColorRed
•Checked, True/False Enabled, True/False
•Visible, True/False
12
Menu Design Standards
• Follow the industry standards for
Windows for names,
order/location, access keys,
shortcut keys
• Basic Main Menus
13
Writing a simple code.
• Double click on the “Exit”
• Write the above code.
• Note: you need to write code
for each of the items in the
menu.
Working with Container
Controls in VB.NET
1. Select the tab Control
2. Go to Property Tab Pages Collection
Windows Common
Dialog Boxes (dlg prefix)
•Predefined standard dialog boxes for:
• File Open and Saving
• Printing and Previewing
• Color selection
• Font selection
•Add the Common Dialog control to form
• Appears in the Component Tray, pane at bottom
of Form Designer where nondisplay controls are
shown
17
Common dialog class inheritance
Common
Dialog
Color Font Print Page Setup
File Dialog
Dialog Dialog Dialog Dialog
Open File
Dialog
Save File
Dialog
ColorDialog
FontDialog
OpenFile Dialog
General Procedures
• 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. Sub procedures are
procedures that do not return any value.
• Function performs actions AND returns a value
23
Creating & Using Sub Procedures
• In the Editor Window enclose the lines of code with
Private Sub and End Sub statements
• Modifiers − specify the access level of the procedure; possible values
are - Public, Private, Protected, Friend, Protected Friend and
information regarding overloading, overriding, sharing, and
shadowing.
• SubName − indicates the name of the Sub
• ParameterList − specifies the list of the parameters
Syntax: Example:
[Modifiers] Sub SubName [(ParameterList)]
[Statements]
End Sub
24
Calling Event Procedures
•Reusable code
•General Form
• [Call] ProcedureName ( )
•Examples
• Call btnCalculate_Click (x)
OR
• btnCalculate_Click (x)
25
Passing Arguments to
Procedures
• Declare as local variable in
1st procedure (calling
procedure)
• Must be declared locally as
same data type expected
by Sub Procedure (called
procedure)
• Name of local variable does
not need to match name in
Sub Procedure argument
list
• Number of arguments and
order must match
26
Passing 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
27
Sub Procedure Example 2
Functions versus Sub Procedures
•Sub Procedures
• Can receive passed values (arguments)
• Performs actions
•Functions
• Can receive passed values (arguments)
• Performs actions
• Returns a value of a specific data type to the
procedure that called it originally
29
Creating & Using Functions
• 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
Function FunctionName ( ) As Datatype
' Statements to execute
End Function
30
Functions - Return Values
• 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
31
Function without return statement
•Syntax:
[Modifiers] Function FunctionName [(ParameterList)] As
ReturnType
[Statements]
End Function
Function with return statement
Function Example
34
Passing value from Form1 to
Form2
Passing value from Form1 to
Form2