Example 1.5a: An About Box.
Example 1.5b: A switchboard form to control an application with multiple forms.
Example 1.5c: An Urn Problem.
Problem 1.5: Medical Testing.
Write a program that takes as input the prevalence of a disease P(D+) as well as the sensitivity P(T+|D+) and specificity P(T-|D-) of a test for the disease, and outputs the False Positive Rate (FPR) = P(T+|D-), the False Negative Rate (FNR) = P(T-|D+), the Positive Predictive Power (PPP) = P(D+|T+), and the Negative Predictive Power (NPP) = P(D-|T-) of the test. Set the defaults for the prevalence, sensitivity, and specificity of the test to be .1, .95, and .95, respectively. Include a menu system with a File Menu that has an exit submenu, and a Help Menu with a Definitions submenu that displays a message box with the definitions of FPR, FNR, etc., and a submenu that displays an About Box.

Event Procedures:
' In (Declarations) section of (General)
' Programmer: R. Berman
' Date: May, 1996
' Title: About Form
' Description: This form contains 4 labels which supply
' information about the form that calls it.
' The labels give the a title of the calling form,
' the programmer's name, the version number
' of the program, and the date it was last
' updated. The form used to call the about form
' sets the caption of the form and these 4 labels
' to appropriate strings. There is also an image
' control in which any suitable image may be
' placed (using the picture property), as well as a
' Line and a Rectangle which are included for aesthetics.
' Version Microsoft Visual Basic 4.0
Private Sub cmdOK_Click()
' Hide this form
frmAbout.Hide
End Sub
Private Sub Form_Load()
' Center the form in the screen.
' Note that the default object is this form
' so, for example, 'Top' is understood to
' mean frmAbout.Top
Top = (Screen.Height - Height) / 2
Left = (Screen.Width - Width) / 2
End Sub
Object, Property and Settings:
Object Property Setting
Form Name frmAbout
Caption "About the About Form"
ControlBox 0 'False
MaxButton 0 'False
CommandButton Name cmdOK
Caption "&OK"
Image Name Image1
Picture (Metafile)
Stretch -1 'True
Label Name lblDate
Alignment 2 'Center
BackStyle 0 'Transparent
Caption "May, 1996"
Shape Name Shape1
Label Name lblVersion
Alignment 2 'Center
BackStyle 0 'Transparent
Caption "Version 1"
Label Name lblProgrammer
Alignment 2 'Center
BackStyle 0 'Transparent
Caption "Written by R. Berman"
Label Name lblTitle
Alignment 2 'Center
BackStyle 0 'Transparent
Caption "The About Form"
Line Name Line1
BorderWidth 5



Event Procedures:
' In (Declarations) section of (General)
' Programmer: R. Berman
' Date: May, 1996
' Title: Bayes' Formula
' Description: This project gives solutions to
' programming problems involving Bayes'
' Formula. This is a "switchboard" form
' which allows the user to choose one
' of the two applications which use Bayes'
' Formula, the Urn Problem and the
' Medical Testing Problem. This form
' gives a choice between using command
' buttons or menu choices. Also included
' are two submenus under the "Help" menu,
' the first of which displays a message
' box with Bayes' Formula, and the second
' of which displays an "About" Box.
' Version Microsoft Visual Basic 4.0
' Require explicit declaration of variables
Option Explicit
Private Sub cmdExit_Click()
' End the application
End
End Sub
Private Sub cmdMedTest_Click()
' Hide this form and show the form for the
' medical testing problem. This is accomplished
' by calling the corresponding menu click procedure.
mnuFileMedTest_Click
End Sub
Private Sub cmdUrnProblem_Click()
' Hide this form and show the form for the
' Urn Problem. This is accomplished by
' calling the corresponding menu click procedure.
mnuFileUrnProb_Click
End Sub
Private Sub Form_Load()
' Center the form in the screen.
' Note that the default object is this form
' so, for example, 'Top' is understood to
' mean frmBayesMain.Top
Top = (Screen.Height - Height) / 2
Left = (Screen.Width - Width) / 2
End Sub
Private Sub mnuFileExit_Click()
' End the application
End
End Sub
Private Sub mnuFileMedTest_Click()
' Hide this form and show the Medical
' Testing form
Hide
frmMedicalTesting.Show
End Sub
Private Sub mnuFileUrnProb_Click()
' Hide this form and show the form for the
' Urn Problem
Hide
frmUrnProbs.Show
End Sub
Private Sub mnuHelpAbout_Click()
' Set the caption property of the About Form
' and various labels which supply information.
' First assign the string consisting of "About "
' and the caption of this form (Bayes' Formula) to
' be the caption of the about form.
frmAbout.Caption = "About " & Caption
' Next there are 4 labels in the about form which need
' to be filled in with information relevant to the
' form which it is describing. Note that to refer to
' contorls on a different form the prefix "formname!"
' must be added to the usual "controlname.property"
' syntxa, so the complete expression takes the form
' "formname!controlname.property".
' The first command below adds the word "Examples" to the
' caption of the the form which calls it and assigns
' it to the first label;
frmAbout!lblTitle.Caption = Caption & " Examples"
' the second assigns the programmer's name to the
' corresponding label;
frmAbout!lblProgrammer.Caption = "Written by R. Berman"
' the third gives the version of the program;
frmAbout!lblVersion.Caption = "Version 1"
' the fourth gives the date the program was last
' revised.
frmAbout!lblDate.Caption = "May, 1996"
' Open the About Form.
frmAbout.Show
End Sub
Private Sub mnuHelpBayes_Click()
' Declare 3 variables for the arguments of the
' message box. The first is the actual message,
' in this case Bayes' Formula; the second is a
' number determining the icon exhibited in the
' box (we use the built-in VB constant
' vbInformation which equals 64); the third is
' the title of the message box.
Dim st_Msg As String
Dim i_MsgDialog As Integer
Dim st_Title As String
' Also define a variable which causes the
' message to continue on the next line;
' these are the carriage return and line
' feed characters.
Dim st_CRLF As String
st_CRLF = Chr$(10) & Chr$(13)
' Now define a string that will display Bayes' Formula
' This uses the 'Space(number)' function which returns
' spaces repeated the given number of times, and the
' 'String(number, character)' function which repeats the
' character the given number of times. These are used to
' align the formula properly in the message box.
st_Msg = "For events A and B, Bayes' formula says: "
st_Msg = st_Msg & st_CRLF & st_CRLF
st_Msg = st_Msg & Space(30) & "P(A|B) P(B)" & st_CRLF
st_Msg = st_Msg & _
Space(7) & "P(B|A) = " & String(40, "-") & st_CRLF
st_Msg = st_Msg & Space(21) & "P(A|B) P(B) + P(A|B') P(B')"
' Define the second and third arguments of the MsgBox procedure.
i_MsgDialog = vbInformation
st_Title = "Bayes' Formula"
' Display the message box.
MsgBox st_Msg, i_MsgDialog, st_Title
End Sub
Object, Property and settings:
Object Property Setting
Form Name frmBayesMain
Caption "Bayes' Formula"
Appearance 0 'Flat
CommandButton Name cmdMedTest
Caption "&Medical Testing"
BackColor &H80000005&
Appearance 0 'Flat
CommandButton Name cmdExit
Appearance 0 'Flat
BackColor &H80000005&
Caption "E&xit"
CommandButton Name cmdUrnProblem
Appearance 0 'Flat
BackColor &H80000005&
Caption "&Urn Problem"
Menu Name mnuFile
Caption "&File"
Menu Name mnuFileUrnProb
Caption "&Urn Problem"
Shortcut ^U
Menu Name mnuFileMedTest
Caption "&Medical Testing"
Shortcut ^M
Menu Name mnuFileSep1
Caption "-"
Menu Name mnuFileExit
Caption "&E&xit"
Shortcut ^X
Menu Name mnuHelp
Caption "&Help"
Menu Name mnuHelpBayes
Caption "&Bayes Formula"
Shortcut ^B
Menu Name mnuHelpAbout
Caption "&About"
Shortcut ^A


Event Procedures: ' In (declarations) section of (General) ' ' Programmer: R. Berman ' Date: May, 1996 ' Title: An Urn Problem ' Description: This program computes probabilities ' associated with the following probability ' experiment. Two urns, Urn 1 and Urn 2 ' contain white and red balls. An urn is chosen, ' and then a ball is chosen from the urn, ' The probability of choosing Urn 1 and the ' proportion of white balls in each urn are ' input by the user. This program determines the ' probability that: ' (1) the ball drawn is white, and ' (2) the ball came from Urn 1 given that it ' is white. ' Version: Microsoft Visual Basic 4.0 ' All of the following probabilities are declared as ' module-level variables with single-precision datatype. ' We have chosen create them with module-level scope in order ' to make them easily accessible to different procedures that ' will be using them. It also would have been possible to define ' them locally within a procedure and pass them to other procedures ' as arguments of the procedures. ' The definition of the variables as module-level is accomplished ' by declaring them here in the (Declarations) section of (General). ' We use the prefix "m" in their identifiers (names) to conform to ' the naming convention and serve as a reminder that they are ' module-level variables. ' ms_ProbUrn1 = the probability of choosing Urn 1 ' ms_PropWhiteUrn1 = the proportion of white balls in Urn 1 ' ms_PropWhiteUrn2 = the proportion of white balls in Urn 2 Dim ms_ProbUrn1 As Single Dim ms_PropWhiteUrn1 As Single Dim ms_PropWhiteUrn2 As Single
' Require explicit declaration of variables. Option Explicit
Private Sub ClearOutput()
' This procedure is used to clear the output labels
' after the cursor leaves one of the input Text Boxes.
' Declare an index variable of integer datatype.
Dim i_Index As Integer
' Clear the two output labels. Note that these have been defined
' as an array of Label controls lblOutput(0) and lblOutput(1).
For i_Index = 0 To 1
lblOutput(i_Index).Caption = ""
Next i_Index
End Sub
Private Sub cmdCompute_Click()
' Get the user input for ' ms_ProbUrn1 = the probability of choosing Urn 1 ' ms_PropWhiteUrn1 = the proportion of white balls in Urn 1 ' ms_PropWhiteUrn2 = the proportion of white balls in Urn 2 ' Input is obtained by means of the 'GetInput' function which ' returns a value of True (-1) if the input satisfies the ' input criteria and returns False (0) otherwise. In the latter ' case, a message box is also displayed indicating that there is ' an error. If b_GetInput() Then
' Output the results based on formulas given above. OutputResults
End If End Sub
Private Function b_GetInput() As Boolean
' The prefix "b" of the function name indicates that it returns a
' value of Boolean datatype. The return value is True (-1) if the
' input satisfies the input criteria and False (0) otherwise.
' In the latter case, a message box is displayed to indicate the error.
' For the message box, declare 3 variables for its arguments.
' The first is for the actual error message, the second is a
' number determining the icon exhibited in the box (we use the
' built-in VB constant vbExclamation which equals 48), and the
' third is the title of the message box.
Dim st_Msg As String
Dim i_MsgDialog As Integer
Dim st_Title As String
' Define the error string.
st_Msg = "Input probabilities and proportions between 0 and 1."
' Define the type of dialog box.
i_MsgDialog = vbExclamation
' Define the message box title.
st_Title = "Change Input"
' Get the user input of ms_ProbUrn1, ms_txtPropWhiteUrn1,
' and ms_txtPropWhiteUrn2
ms_ProbUrn1 = Val(txtProbUrn1.Text)
ms_PropWhiteUrn1 = Val(txtPropWhiteUrn1.Text)
ms_PropWhiteUrn2 = Val(txtPropWhiteUrn2.Text)
' Do a basic check that the input is appropriate.
' The requirement is that the probability and proportions
' are between 0 and 1.
If (ms_ProbUrn1 > 0) And (ms_ProbUrn1 < 1) And _
(ms_PropWhiteUrn1 > 0) And (ms_PropWhiteUrn1 < 1) And _
(0 < ms_PropWhiteUrn2) And (ms_PropWhiteUrn2 < 1)
Then
GetInput = True
Else
GetInput = False
' Display message Box with error message.
MsgBox st_Msg, i_MsgDialog, st_Title
End If
End Function
Private Sub OutputResults()
' With
'
' A = the event that Urn 1 is chosen,
' B = the event that the ball chosen is white,
'
' and A' and B' the complements of these sets,
' we apply the formula
'
' P(B) = P(A) P(B|A) + P(A') P(B|A')
'
' and Bayes' formula
'
' P(AB) P(B|A) P(A)
' Pr(A|B) = ------ = ------------
' P(B) P(B)
'
' where
'
' P(A) = the probability that Urn 1 is chosen
' = ms_ProbUrn1
' P(B|A) = the proportion of White Balls in Urn 1
' = ms_PropWhiteUrn1
' P(B|A') = the proportion of White Balls in Urn 2
' = ms_PropWhiteUrn2
'
' The output is formatted with 2 decimal points.
lblOutput(0).Caption = _ Format$(ms_ProbUrn1 * ms_PropWhiteUrn1 + _
(1-ms_ProbUrn1) * ms_PropWhiteUrn2, "0.00") lblOutput(1).Caption = _
Format$(ms_ProbUrn1 * ms_PropWhiteUrn1 / _
(ms_ProbUrn1 * ms_PropWhiteUrn1 + (1-ms_ProbUrn1) * ms_PropWhiteUrn2), "0.00")
End Sub
Private Sub Form_Load()
' Center the form in the screen. Top = (Screen.Height - Height) / 2 Left = (Screen.Width - Width) / 2 End Sub
Private Sub mnuFileDefaults_Click() ' Declare an index variable of integer datatype. Dim i_Index As Integer ' Return settings to defaults. txtProbUrn1.Text = ".5" txtPropWhiteUrn1.Text = ".1" txtPropWhiteUrn2.Text = ".6" ' Clear the output labels. For i_Index = 0 To 1 lblOutput(i_Index).Caption = "" Next i_Index
End Sub
Private Sub mnuFileExit_Click()
' End the program.
End
' Note: If you are using a main switchboard form, you would hide this ' form and show the main form with the following commands: ' Hide ' frmBayesMain.Show End Sub
Private Sub mnuHelpAbout_Click()
' Set the caption property of the About Form ' and various labels which supply information. frmAbout.Caption = "About " & Caption frmAbout!lblTitle.Caption = Caption frmAbout!lblProgrammer.Caption = "Written by R. Berman" frmAbout!lblVersion.Caption = "Version 1" frmAbout!lblDate.Caption = "May, 1996" ' Open the About Form. frmAbout.Show End Sub Private Sub txtProbUrn1_LostFocus()
' After leaving this Text Box, clear all output labels. ClearOutput
End Sub
Private Sub txtPropWhiteUrn1_LostFocus()
' After leaving this Text Box, clear all output labels.
ClearOutput
End Sub
Private Sub txtPropWhiteUrn2_LostFocus()
' After leaving this Text Box, clear all output labels. ClearOutput
End Sub
Object, Property and settings:
Object Property Setting
Form Name frmUrnProbs
Appearance 0 'Flat
BackColor &H00400000&
BorderStyle 1 'Fixed Single
Caption "An Urn Problem"
ForeColor &H00FFFFFF&
TextBox Name txtProbUrn1
Appearance 0 'Flat
TabIndex 2
Text ".5"
TextBox Name txtPropWhiteUrn1
Appearance 0 'Flat
TabIndex 4
Text ".1"
TextBox Name txtPropWhiteUrn2
Appearance 0 'Flat
TabIndex 6
Text ".6"
Label Name Label1
Alignment 2 'Center
AutoSize -1 'True
BorderStyle 1 'Fixed Single
Caption "2 Urns each contain White balls
and Red balls. An urn is selected
and then a ball is selected at random
from the urn."
TabIndex 0
WordWrap -1 'True
Label Name Label2
Alignment 1 'Right Justify
Appearance 0 'Flat
BackColor &H0000FFFF&
BackStyle 0 'Transparent
Caption "Probability of choosing Urn 1:"
ForeColor &H0000FFFF&
TabIndex 1
Label Name Label3
Alignment 1 'Right Justify
Appearance 0 'Flat
BackColor &H0000FFFF&
BackStyle 0 'Transparent
Caption "Proportion of White balls in Urn 1:"
ForeColor &H0000FFFF&
TabIndex 3
Label Name Label4
Alignment 1 'Right Justify
Appearance 0 'Flat
BackColor &H0000FFFF&
BackStyle 0 'Transparent
Caption "Proportion of White balls in Urn 2:"
ForeColor &H0000FFFF&
TabIndex 5
Label Name Label5
Alignment 2 'Center
Appearance 0 'Flat
BackColor &H0000FFFF&
BackStyle 0 'Transparent
Caption "White"
ForeColor &H0000FFFF&
TabIndex 7
Label Name Label6
Alignment 2 'Center
Appearance 0 'Flat
BackColor &H0000FFFF&
BackStyle 0 'Transparent
Caption "Urn 1 Given White"
ForeColor &H0000FFFF&
TabIndex 8
Label Name Label7
Alignment 1 'Right Justify
Appearance 0 'Flat
BackColor &H0000FFFF&
BackStyle 0 'Transparent
Caption "Probability:"
ForeColor &H0000FFFF&
TabIndex 9
Label Name lblOutput
Alignment 2 'Center
BackColor &H00000080&
BorderStyle 1 'Fixed Single
ForeColor &H00FFFFFF&
Index 0
TabIndex 10
Label Name lblOutput
Alignment 2 'Center
BackColor &H00000080&
BorderStyle 1 'Fixed Single
ForeColor &H00FFFFFF&
Index 1
TabIndex 11
CommandButton Name cmdCompute
Appearance 0 'Flat
BackColor &H80000005&
Caption "&Compute"
Default -1 'True
TabIndex 12
Menu Name mnuFile
Caption "&File"
Menu Name mnuFileDefaults
Caption "&Defaults"
Shortcut ^D
Menu Name mnuFileExit
Caption "E&xit"
Shortcut ^X
Menu Name mnuHelp
Caption "&Help"
Menu Name mnuHelpAbout
Caption "&About"
Shortcut ^A
Problem 1.5: Medical Testing.


