Chapter 5' Menus, Sub Procedures, and Sub Functions - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Chapter 5' Menus, Sub Procedures, and Sub Functions

Description:

... sequence, and their data types must match in both locations, but not the names ... Flags = cdlCCRGBInit Initialize the color dialog box. ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 15
Provided by: faculty46
Category:

less

Transcript and Presenter's Notes

Title: Chapter 5' Menus, Sub Procedures, and Sub Functions


1
Chapter 5. Menus, Sub Procedures, and Sub
Functions
  • Menus
  • Menu Commands
  • Use Menu Editor to create Menus
  • The Caption the words you want to appear on the
    screen
  • The Name the name of menu control
  • Naming Standards mnu
  • Submenus another list of menus under a menu

2
Common Dialog Boxes
  • Use a set of predefined standard dialog boxes
    like colors and fonts, printing, opening, and
    saving
  • Invisible when your program runs
  • How to set up Common Dialog Box
  • Open the Project menu
  • Choose Components
  • Check Microsoft Common Dialog Control 6.0

3
  • Show Method General Form
  • object.ShowMethod
  • Method ShowOpen, ShowSave, ShowColor, ShowFont,
    ShowPrint
  • Color Dialog Box
  • dlgCommon.ShowColor Display the Color dialog box
  • Using default color
  • dlgCommon.ShowColor
  • frmMain.BackColor dlgCommon.Color
  • Using currently selected color
  • With dlgCommon
  • .Flags cdlCCRGBInit Initialize the dialog box
  • .Color frmMain.BackColor Set initial color
  • .ShowColor Display dialog box and get color
  • End With
  • frmMain.BackColor dlgCommon.Color Set color of
    form

4
  • Font Dialog Box
  • Font Property of Common Dialog Control
  • FontBold, FontItalic, FontName, FontSize,
    FontStrikeThru, FontUnderline
  • The value of the Flags property
    cdlCFScreenFonts, cdlCFPrinterFonts, cdlCFBoth
  • With dlgCommon
  • .Flags cdlCFScreenFonts
  • .ShowFont
  • End With
  • With lblEmployee.Font
  • .Name dlgCommon.FontName
  • .Bold dlgCommon.FontBold
  • End With

5
Writing General Procedures
  • Useful in breaking down large sections of code
    into smaller units that perform a specific task
  • Useful either a command button or a menu option
    to do the same thing
  • Can be called from either a command button or a
    menu option

6
Passing Values to Procedures
  • Passing Arguments ByVal
  • Sends a copy of the arguments value to the
    procedure so that the procedure cannot alter the
    original value
  • The number of arguments, their sequence, and
    their data types must match in both locations,
    but not the names
  • Passing Arguments ByRef
  • Sends a reference indicating where the value is
    stored in memory, allowing the called procedure
    to actually change the arguments original value
  • Default method if not specified ByVal or ByRef

7
  • Private Sub SelectColor (lngIncomingColor As
    Long)
  • With dlgColor
  • .Flags cdlCCRGBInit Initialize the color
    dialog box
  • .Color lngIncomingColor Set the initial
    color
  • .ShowColor
  • End With
  • End Sub
  • --------------------------------------------------
    ----------------------------------
  • Private Sub cmdChangeMessage_Click()
  • Dim lngOriginalColor As Long
  • lngOriginalColor lblMessage.ForeColor
  • SelectColor lngOriginalColor
  • lblMessage.ForeColor dlgColor.Color
  • End Sub

8
  • Private Sub SelectColor (ByVal lngIncomingColor
    As Long)
  • With dlgColor
  • .Flags cdlCCRGBInit Initialize the color
    dialog box
  • .Color lngIncomingColor Set the initial
    color
  • .ShowColor
  • End With
  • End Sub
  • --------------------------------------------------
    ----------------------------------
  • Private Sub cmdChangeMessage_Click()
  • Dim lngOriginalColor As Long
  • lngOriginalColor lblMessage.ForeColor
  • SelectColor lngOriginalColor
  • lblMessage.ForeColor dlgColor.Color
  • End Sub

9
Function Procedures versus Sub Procedures
  • Sub Procedure is a procedure that performs
    actions and it also can return multiple values
  • Function procedure may perform an action, but it
    also returns a value (the return value) to the
    point from which it was called

10
  • Private Function curCommission (ByVal
    curSalesAmount As Currency) As Currency
  • If curSalesAmount lt 1000 Then
  • curCommission 0
  • ElseIf curSalesAmount lt 2000 Then
  • curCommission 0.15 curSalesAmount
  • Else
  • curCommission 0.2 curSalesAmount
  • End If
  • End Function
  • --------------------------------------------------
    -----------------------
  • Private Sub cmdCommission_Click ()
  • Dim curSales As Currency
  • If IsNumeric (txtSales.Text) Then
  • curSales Val(txtSales.Text)
  • lblCommission.Caption curCommission(curSales)
  • End If
  • End Sub

11
  • Writing a Function with Multiple Arguments
  • Private Function curPayment(curRate As Currency,
    _
  • curTime As Currency, curAmt As Currency) As
    Currency
  • curPayment curAmt (1 curRate / 12)
    (curTime 12)
  • End Function
  • --------------------------------------------------
    --------------------------
  • Calling a Function with Multiple Arguments
  • Private Sub cmdPayment_Click ()
  • Dim curRate As Currency
  • Dim curTime As Currency
  • Dim curAmount As Currency
  • curRate Val(txtRate.Text)
  • curTime Val(txtTime.Text)
  • curAmount Val(txtAmount.Text)
  • lblPayment.Caption curPayment (curRate,
    curTime, curAmount)
  • End Sub

12
  • Reusing Procedures
  • Private Sub mnuCalculateBonus_Click()
  • Const curBonusPercent As Currency 0.01
  • If curPurchase gt 0 Then
  • curBonus curCalculateBonus (curPurchase,
    curBonusPercent)
  • End If
  • ----------------------------------------
  • Private Sub mnuCalculateCommission_Click ()
  • Const curCommissionLimit As Currency 500
  • Const curCommissionPercent As Currency 0.05
  • If curPurchase gt curCommissionLimit Then
  • curCommission curCalculateBonus (curPurchase,
    _
  • curCommissionPercent)
  • End If
  • ------------------------------------------
  • Private Function curCalculateBonus (curAmount As
    Currency, curRate As _ Currency) As Currency
  • curCalculateBonus curAmount curRate
  • End Function

13
  • Breaking Calculations into Smaller Units
  • Private Sub mnyFileCalc_Click()
  • curAverage curFindAverage (intGame1, intGame2,
    intGame3)
  • intSeries intFindSeries(intGame1, intGame2,
    intGame3)
  • curHandicap curFindHandicap (intGame1,
    intGame2, intGame3)
  • strHighGame strFindHighGame (intGame1,
    intGame2, intGame3)
  • ---------------------------------------------
  • Public Function curFindAverage(intScore1 As
    Integer, intScore2 As Integer, _ intScore3 As
    Integer) As Currency
  • curFindAverage (intScore1 intScore2
    intScore3)/3
  • -----------------------------------
  • Private Function curFindHandicap (curAverage As
    Currency) As Currency
  • curFindHandicap (200 curAverage) 0.8
  • ------------------------------------
  • Private Function intFindSeries(intGame1 As
    Integer, _
  • intGame2 As Integer, intGame3 As Integer) As
    Integer
  • intFindSeries intGame1 intGame2 intGame3

14
  • Private Function strFindHighGame(intGame1 As
    Integer, _
  • intGame2 As Integer, intGame3 As Integer) As
    Integer
  • If (intGame1 gt intGame2) Then
  • If (intGame1 gt intGame3) Then
  • strFindHighGame 1
  • Else
  • strFindHighGame 3
  • End If
  • ElseIf (intGame2 gt intGame3) Then
  • strFindHighGame 2
  • Else
  • strFindHighGame 3
  • End If
  • End Function
Write a Comment
User Comments (0)
About PowerShow.com