file=5-menus; updated 2/20/2003 

Menus, Common Dialog Controls,

Context Menus, Sub Procedures, and Functions

OBJECTIVES:

·       Create menus and submenus.

·       Use Windows common dialog boxes

·       Write sub procedures and functions.

·       Call procedures.


 

Menus

Examine any Microsoft software product and you'll find a fairly standard menu that includes a menu bar with drop down lists of menu items. 

 

Menu items are often used to complement buttons to activate procedures.  Menus are controls with properties and methods and events, such as the Name and Text properties and Click event.

 

Defining Menus

Menus are added to a form by adding a MainMenu control from the toolbox.  The MainMenu control will appear in the component tray below the form.

 

Figure 5.1 shows a menu being added.  Note the words Type Here that appear below and to the right of a menu name.  Clicking elsewhere on the form deactivates the Menu Designer.  Clicking the menu again activates the Menu Designer.

Figure 5.1

 

Text Property

A menu's Text property stores the words displayed for a menu.  Type it directly into the menu or in the Property window.

 

The standard for ordering top-level menus is:  File, Edit, and View (if used) followed by any specialty top-level menu items.  The last two menus are always Window and Help if they are used.

 

Typing an ampersand (&) before a letter causes it to be underlined and to be a hot key for keyboard access by the Alt+letter method.  Example:  &File.

Name Property

Name the menus through the property window.  Use a three-letter prefix of mnu to name menus, for example the File menu will be named mnuFile.

Submenus

Menus are drop-down items below a menu name.  Submenus are new lists associated with each menu item.  See Figure 5.2 for the Color and Font submenu items.

 

Menu items are named after the top-level menu name, for example, the Exit menu below File will be named mnuFileExit.

Figure 5.2

 

Separator Bars

A Separator Bar is a line drawn across a menu to separate items by purpose.  Create them by:

-         Type a single hyphen (-) for the text, or

-         Right-click on the Menu Designer where you want a separator bar and choose Insert Separator from the context menu. 

 

Do not bother naming separator bar menu items.

 


In-Class Exercise

 

Startup a new project named Ch5Inclass.  The form's Text property value is Chapter 5 In Class (use your own name). 

 

Create the menus shown below for a new project.

·       &File

o      E&xit

·       &Edit

o      &Style

§       &Color

-         (adding a blank line here later)

§       &Font

·       &Display

o      &Summary

·       &Help

o      &About

 

Name the menus items appropriately.


 

Coding a Menu Click Event

Each menu command has a Click event.  You can write any code you want to execute for a menu command in menu command's Click event procedure.

 

Code the Exit menu click event to end the project's execution (the menu should be named mnuFileExit).

 

Code the Help-About menu click event to display a message box like the following.

 

Private Sub mnuHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpAbout.Click

    'Display about message box

    Dim strMessage

    strMessage = "Programmed by Doug Bock"

    MessageBox.Show(strMessage, "About My Program", _

        MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub


Figure 5.3

 

Modifying a Menu

Modify a menu by clicking on the menu at design time.  It's very simple.

 

Right-click the form's menu bar – produces a context menu that provides options to Delete, Insert, New, Insert Separate, and Edit Names.  You can also drag and drop menu items.

Enabled Property

All menus default their Enabled property to True.

 

You can set the Enabled property at design time with the Property window or in code as shown here:

 

mnuDisplaySummary.Enabled = False

 

Checked Property

A check mark beside a menu items indicates the item is checked.  The Checked property defaults to False.

 

You can set the Checked property at design time or in code as shown here:

 

mnuDisplaySummary.Checked = True

 

Suppose that the Summary menu displays a summary form with information for the system user, but the system user wants to be able to display or hide the form.  Further, you want to coordinate the Checked property of the menu with displaying or hiding the form.

 

Private Sub mnuSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSummary.Click

    'Hide/display summary

    If mnuDisplaySummary.Checked = True Then

        'Hide summary

        mnuDisplaySummary.Checked = False

    Else

        'Show summary

        mnuDisplaySummary.Checked = True

    End If

End Sub

 


In-Class Exercise Cont.

 

Add the code shown above to check and uncheck the Summary menu.

Test the program.


Common Dialog Boxes

Windows and VB offer predefined, standard dialog boxes for tasks such as:

·       Colors and Fonts

·       Printing

·       Opening and Saving files.

 

The Common Dialog Controls available are named:

·       OpenFileDialog

·       SaveFileDialog

·       FontDialog

·       ColorDialog

·       PrintDialog

·       PrintPreviewDialog

 

Add a Common Dialog Control to the form.  Use the prefix dlg to name the control, Example: dlgColor.

 

Use the ShowDialog method to display a dialog box, Example:

 

dlgColor.ShowDialog()

 

Color Dialog Box

Figure 5.4 shows a Color Dialog Box. 

Figure 5.4

 

When you select a color, its value is saved to the Color property of the common dialog control.  You can then assign the color value to one of the ForeColor or BackColor properties of another control, Example:

 

txtName.ForeColor = dlgColor.Color

 


In-Class Exercise Cont.

Color Dialog Testing

·  Add a label and text box control to the In-Class Exercise form.  Name the textbox txtName.

·  Add a Color Dialog Control to the form – the control is in the toolbox, you have to scroll down for it.  Name it dlgColor.

Figure 5.5

 

·  Write code for the click event of the mnuEditStyleColor submenu object to change the ForeColor property of the textbox.

 

Private Sub mnuEditStyleColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditStyleColor.Click

    'Change color of textbox text

    With dlgColor

        'Assign current color to Color Dialog Control

        .Color = txtName.ForeColor

        .ShowDialog()

        'Assign selected color to text box

        txtName.ForeColor = .Color

    End With

End Sub

 


Font Dialog Box

Figure 5.6 shows the Font Common Dialog Box. 

Figure 5.6

 

 

After a selection is made, the Font property of another control can be assigned the selected font.

 

Private Sub mnuEditStyleFont_Click(ByVal sender As System.Object, byVal e As System.EventArgs) Handles mnuEditStyleFont.Click

    'Change font

    With dlgFont

        'Store current font to common dialog control

        .Font = txtName.Font

        .ShowDialog()

        txtName.Font = .Font  'Store new font to textbox

    End With

End Sub

 

The other font properties (bold, italic, underline) are read-only and require code beyond that covered in this chapter to modify at runtime.

 


In-Class Exercise Cont.

Font Dialog Testing

·  Add a Font Dialog Control to the form – the control is in the toolbox, you have to scroll down for it.  Name it dlgFont.

·  Using the code shown above, enable changing the font for the txtName text box control.

 


Context Menus

Context menus popup when you right-click an item – they tell you about options or information about the item selected.

 

Context menus are created similar to a menu bar – you add a ContextMenu Control that displays in the component tray below the form.

 

Figure 5.7 shows a context menu on a form – clicking the words Context Menu displays a Type Here menu item.  You can add menus such as &Color, &Font, and E&xit. 

Figure 5.7

 

To attach the context menu to an object, such as the project form:

·  Name the context menu a unique name such as mnuContext.

·  Set the text box's ContextMenu property to mnuContext.

 

The individual menus on the context menu need unique names, such as mnuContextColor, mnuContextFont, and mnuContextExit.  Select the menu items and use the Property window to change the names.

 

You must also code the menu items.  In the Exit context menu click event, the keyword Me refers to the form.  Closing the form terminates the project.

 

Private Sub mnuContextExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuContextExit.Click

    'End project

    Me.Close()  'Closes current form.

End Sub

 

In the Color and Font menu click events, the mnuContext.SourceControl property of the context menu references the control which the mouse is pointing to at the time that it is right-clicked. 

 

Private Sub mnuContextColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuContextColor.Click

    'Change color of object 

    With dlgColor

        'Send color to dialog box

        .Color = mnuContext.SourceControl.ForeColor

        .ShowDialog()

        'Display new color

        mnuContext.SourceControl.ForeColor = .Color

    End With

End Sub

 

Private Sub mnuContextFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuContextFont.Click

    'Change font of object

    With dlgFont

        'Send font to dialog box

        .Font = mnuContext.SourceControl.Font

        .ShowDialog()

        'Display new font

        mnuContext.SourceControl.Font = .Font

    End With

End Sub

 


In-Class Exercise Cont.

Context Menu Coding

·  Add a context menu named mnuContext to your project.

·  Add context menu items as shown above for Color, Font, and Exit and name them as shown above.

·  Add code for the click event of the context menu items and test the project.

 


Writing Procedures

Sometimes a programming module will become quite large, and it makes sense from a logical design perspective to divide the module into two or more separate procedures.

 

At other times you will have a procedure that you need to "reuse" by calling it from either a context menu or regular menu or button that is clicked.

 

General procedures fill these needs.  There are two types: 

·  Sub procedures that perform actions, but are not associated with any specific event such as a click event.

·  Functions that perform actions and return a value.

 

Creating a Sub Procedure

Typing the line:

 

Private Sub ProcedureName

 

will cause the system to add parentheses and the End Sub statement.  You can then add your code inside the procedure.

 

In addition to Private, there are Public, Friend, and Protected procedures that are covered in later notes.

 

A procedure to select colors is:

 

Private Sub SetColor(IncomingColor As Color)

    'User selects color from dialog control

    With dlgColor

        .Color = IncomingColor 'Store initial color

        .ShowDialog()

    End With

End Sub

 

The IncomingColor variable is termed a parameter.  It is declared as type Color in the parameter list (inside parentheses) of the sub procedure.

 

The existing procedures mnuEditStyleColor_Click and mnuContextColor_Click can be modified and simplified to call (use) the SetColor sub procedure.

 

Here the ForeColor property of txtName is passed as a parameter to the SetColor procedure which receives the value of the color and stores it to the IncomingColor variable.

 

Private Sub mnuEditStyleColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditStyleColor.Click

    'Change color of textbox text by sending the

    'ForeColor property to the SetColor sub procedure

    SetColor(txtName.ForeColor)

    'Store new color to the control

    txtName.ForeColor = dlgColor.Color

End Sub

 

Here the ForeColor property of the selected control on the form is sent as a parameter value to the SetColor sub procedure.

 

Private Sub mnuContextColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuContextColor.Click

    'Change color of object by sending the color to

    'the SetColor sub procedure

    SetColor(mnuContext.SourceControl.ForeColor)

    'Store new color to the control

    mnuContext.SourceControl.ForeColor = dlgColor.Color

End Sub

 

Learn these rules about sub procedure arguments:

·  Arguments passed to a sub procedure must be the same type of data as the sub procedure expects to receive, i.e., you wouldn't pass a football player a beach ball – you'd pass a football!.

·  Argument values must be passed if the sub procedure has an argument list.

·  A sub procedure can have as many arguments as are needed, and the sequence and data type must match between the calling and called procedures.

 

ByVal versus ByRef

Arguments can be passed ByVal or ByRef. 

·  ByVal sends a copy of the argument's value to the procedure so the procedure does not alter the original copy of the value.

·  ByRef sends the memory location of the value to the procedure so that if the value is modified, the original copy is modified.

·  ByVal is the default if it is not specified.

 

Examples:

 

Private Sub SetColor(ByVal IncomingColor As Color)

Private Sub SetColor(ByRef IncomingColor As Color)

 


In-Class Exercise Cont.

Sub Procedure Coding

·  Add a sub procedure named SetColor to your project.

·  Modify the existing procedures mnuEditStyleColor_Click and mnuContextColor_Click to call (use) the SetColor sub procedure.

 


Creating a Function

We have used functions such as FormatCurrency and IsNumeric.  Functions always return a value. 

 

FormatCurrency returns a formatted string of characters to display to a label or textbox.  IsNumeric returns a Boolean value of True or False.

 

You can create your own functions in programs to perform common tasks, such as computing sales tax or a salesperson's commission amount. 

 

Create a function by typing:

 

Private Function FunctionName() As Datatype

 

You must specify the Datatype that the function returns.  This function computes the sales commission amount.  The parameter passed to the function is the amount of the sale as a decimal value. 

 

Private Function Commission(ByVal decSalesAmount As Decimal) As Decimal

    'Calculate commission based on amount of sale

    If decSalesAmount <= 1000D Then

        Commission = 0.05D * decSalesAmount

    ElseIf decSalesAmount <= 2000D Then

        Commission = 0.1D * decSalesAmount

    Else

        Commission = 0.2D * decSalesAmount

    End If

End Function

 

There are two ways to return a value from a function:

·       Set the function name to the value to be returned inside the function as was done above. 

·       The second is to use the optional Return statement as is done below.

 

Private Function Commission(ByVal decSalesAmount As Decimal) As Decimal

    'Calculate commission based on amount of sale

    If decSalesAmount <= 1000D Then

        Return 0.05D * decSalesAmount

    ElseIf decSalesAmount <= 2000D Then

        Return 0.1D * decSalesAmount

    Else

        Return 0.2D * decSalesAmount

    End If

End Function

Calling a Function

This procedure shows the function named Commission being called within another procedure.

 

Dim decSales As Decimal

Dim decCommissionAmount As Decimal

If IsNumeric(txtSalesAmount.Text) Then

    decSales = CDec(txtSalesAmount.Text)

    'Here is the Call to Commission

    decCommissionAmount = Commission(decSales)   

    lblCommission.Text = _

        FormatCurrency(decCommissionAmount)

End If

 

Just like sub procedures, a function can have none, one, or more than one argument in the parameter list.  The calling and called procedure must pass/receive the same type of data for each argument in the parameter list.

 

Creating and Using a ValidData Function

Functions are often used is to validate data that system users type into textboxes.  Figure 5.8 displays a form that is used to compute the $$$ amount of gross pay due to an employee based on the system user typing the correct number of hours worked (to the nearest tenth of an hour) and hourly pay rate.

Figure 5.8

 

 

Let's assume that the business has three rules that must be enforced for the data typed into the two text boxes:

·       The txtHours value must be numeric.

·       The txtHours value cannot exceed 60 hours (range test).

·       The txtPayRate value must be numeric.

 

Of course you can think of additional data validation rules, but we will limit our discussion to these three.  You can then extend the coding to include additional data validation rules.

 

The ValidData function shown here is declared as a Boolean function meaning it returns a value of either TRUE or FALSE.  The code starts by assuming that the data is NOT valid and sets ValidData to a value of FALSE.  Each rule is tested in turn and if the data is not valid, a message box displays an appropriate message, sets the focus to the text box with the error, and the function returns a value of FALSE.

 

If all of the business rules are satisfied (valid), then ValidData is set to TRUE and the function returns a value of TRUE.

 

Private Function ValidData() As Boolean

    Const sngMAX_HOURS As Single = 60.0

    ValidData = False  'Assume the data is not valid

 

    'Test for hours value numeric

    If IsNumeric(txtHours.Text) = False Then

        MessageBox.Show("Hours value is not numeric.", _

            "Invalid Hours", MessageBoxButtons.OK, _

            MessageBoxIcon.Error)

        txtHours.Focus()

    'Test for hours value not greater than 60

    ElseIf CSng(txtHours.Text) > sngMAX_HOURS Then

        MessageBox.Show("Hours too large", _

            "Invalid Hours", MessageBoxButtons.OK, _

            MessageBoxIcon.Error)

        txtHours.Focus()

    'Test for Pay Rate Numeric

    ElseIf IsNumeric(txtPayRate.Text) = False Then

        MessageBox.Show("Pay Rate is not numeric.", _

            "Invalid Pay Rate", MessageBoxButtons.OK, _

            MessageBoxIcon.Error)

        txtPayRate.Focus()

    Else  'The data is valid

        ValidData = True

    End If

End Function

 

This greatly simplifies the click event for the Compute button because all of the validation code has been moved to the function.  The ValidData function is called as part of the If statement simply by using the function name.  If the function returns a value of TRUE, then the If statement condition is TRUE and the computation takes place; otherwise if the function returns FALSE, then the If statement does not execute.  You may also note that the message boxes that are displayed are displayed as part of the function, not the Compute button's click event.

 

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click

    'Declare variables

    Dim sngHours As Single

    Dim decPayRate, decGrossPay As Decimal

 

    'Compute and display output.  First call the

    'ValidData function to validate the values in

    'the two text boxes.

    If ValidData = True Then

        decPayRate = CDec(txtPayRate.Text)

        sngHours = CSng(txtHours.Text)

 

        'Compute gross pay

        decGrossPay = decPayRate * CDec(sngHours)

 

        'Display output

        lblGrossPay.Text = FormatCurrency(decGrossPay)

    End If

End Sub

 


In-Class Exercise

·       Build the project with the form shown in Figure 5.8. 

·       Write the ValidData function.

·       Write the Compute button's click event code.

·       Test your project.

 


END OF NOTES