A Simple Program

[ Programs ]

This page is going to take you through the steps of making a very simple program.   This program is really simple to create.  It may not do much, but it is useful for demonstrating different aspects of Visual Basic commands.

wpe1A.gif (10993 bytes)

    This program displays a message box using the information supplied by the user.  The text of the message box is taken from the text box under the label "Type your text here!", and the title from the other text box.  It displays a message box with the buttons specified by the selected option button.  For example the settings up there would display the following message box.

wpe1B.gif (2142 bytes)

    Then after the message box has been displayed, and the user clicks on one of the buttons, the return value is shown in the Return Value text box.  This page shows the code of the program, and how it works.

The Code

The Process

Designing The Form

On to the Code

 


The Code

Option Explicit


Private Sub cmdExit_Click()
        End
End Sub

Private Sub cmdMessageBox_Click()
        Dim strText As String
        Dim strTitle As String
        Dim intReturnValue As VbMsgBoxResult
        Dim intButtons As VbMsgBoxStyle

        strText = txtText.Text
        strTitle = txtTitle.Text

        If optOK.Value = True Then
                intButtons = vbOKOnly
        ElseIf optOKCancel.Value = True Then
                intButtons = vbOKCancel
        ElseIf optRetryCancel.Value = True Then
                intButtons = vbRetryCancel
        ElseIf optAbortRetryIgnore.Value = True Then
                intButtons = vbAbortRetryIgnore
        ElseIf optYesNo.Value = True Then
                intButtons = vbYesNo
        Else
                intButtons = vbYesNoCancel
        End If

        intReturnValue = MsgBox(txtText.Text, intButtons, txtTitle.Text)

        Select Case intReturnValue
        Case Is = vbOK
                txtReturnValue.Text = "vbOK"
        Case Is = vbCancel
                txtReturnValue.Text = "vbCancel"
        Case Is = vbAbort
                txtReturnValue.Text = "vbAbort"
        Case Is = vbRetry
                txtReturnValue.Text = "vbRetry"
        Case Is = vbIgnore
                txtReturnValue.Text = "vbIgnore"
        Case Is = vbYes
                txtReturnValue.Text = "vbYes"
        Case Is = vbNo
                txtReturnValue.Text = "vbNo"
        End Select
End Sub


The Process

    First off, you have to create a project file in Visual Basic.   To do that, first start up Visual Basic, you should get a screen something like this.

wpe1C.gif (37794 bytes)

    For this project create a standard .exe file by selecting the standard .exe option, and clicking the open button.  The toolbar (shown below) contains all the different controls that can be placed on the form, and the properties window shows a list of all the different properties of the selected item.  The properties window is where you will change the name of your program, and forms, and all of the controls in the program.  The project explorer is where you'll switch which form you're viewing.

wpe20.gif (4686 bytes)             wpe1F.gif (7265 bytes)

    The first step is to rename the project from "Project1" to "A Simple Program".  you do this by selecting "Project1" in the project explorer, and then changing the "(name)" value to "A Simple Program".  Then you need to change the name of the form to "frmMain".

 

 


Designing the Form

    The first step of designing a form is placing the controls on the form.  To place a new control on a form just double-click on the control that you want on the toolbar.  Then you need to change the "(name)" properties to reflect what those controls are going to do.  And where necessary you should change the captions to something that tells what the function of that control is.  Create a form that looks something like this.

wpe23.gif (11680 bytes)

Control Name Caption Purpose
cmdMessageBox Display Message Box Display the message box.
cmdExit Exit Exit the program.
lblText Type Your Text Here!
txtText Text of the message box.
lblTitle Title:
txtTitle Title of the message box.
lblReturnValue Return Value:
txtReturnValue Return Value of MsgBox()
fraButtons Buttons
optOK OK Display OK button.
optOKCancel OK Cancel Display OK, & Cancel buttons.
optAbortRetryIgnore Abort Retry Ignore Display Abort, Retry, & Ignore buttons.
optRetryCancel Retry Cancel Display Retry, & Cancel buttons.
optYesNoCancel Yes No Cancel Display Yes, No, & Cancel buttons.
optYesNo Yes No Display Yes, & No buttons.

 

 


On to the Code

    The first line of code is simply "Option Explicit".  All this does is tell Visual Basic to force all variables to be defined before they are used.  This is used mostly for purposes of debugging, because if you accidentally misspell a variable name, then Visual Basic will normally just overlook it, and the results of the program might not be what you'd expect.  The next code is the code behind the exit button.  To be able to edit the code for the Exit button click event, just double click on the exit button, or select cmdExit from the object list above the code window.  This will bring up the code window creating the cmdExit_Click() subroutine so you can edit the code.

wpe1C.gif (3740 bytes)

    Behind the Exit button there is one simple command 'End' all it does is close the program.  Next you need to add some code for behind the cmdMessageBox button to display a message box with the properties the user selected.   First you would create the variables to hold the data to send to the MsgBox() function.

        Dim strText As String         'strText - variable to hold the text of the message box
        Dim strTitle As String        'strTitle - variable to hold the title for the message box
        Dim intReturnValue As VbMsgBoxResult        'intReturnValue - to hold the return value
        Dim intButtons As VbMsgBoxStyle        'intButtons - Buttons that the message box will display

 

Next the 'strText' and 'strTitle' variables should be initialized with the values the user inputted in the text boxes.  These are obtained by the 'Text' property of the text boxes.
        strText = txtText.Text        'strText gets the text typed into text box txtText
        strTitle = txtTitle.Text        'strTitle gets the text typed into text box txtTitle

    For the 'intButtons' variable you have to go through the option buttons and test the 'Value' property to find the one that is marked (if it is True then it is, and if it is False then it isn't), and then assign the value that corresponds to the button to the 'intButtons' variable.  You do this in an 'if ... then ... else' statement.

        If optOK.Value = True Then       'if optOK is checked then assign vbOKOnly to intButtons
                intButtons = vbOKOnly
        ElseIf optOKCancel.Value = True Then   '...
                intButtons = vbOKCancel
        ElseIf optRetryCancel.Value = True Then   '...
                intButtons = vbRetryCancel
        ElseIf optAbortRetryIgnore.Value = True Then   '...
                intButtons = vbAbortRetryIgnore
        ElseIf optYesNo.Value = True Then   '...
                intButtons = vbYesNo
        Else   '...
                intButtons = vbYesNoCancel
        End If

    Then to get the value of 'intReturnValue' you have to call the MsgBox() function.  You do so like this.

        intReturnValue = MsgBox(txtText.Text, intButtons, txtTitle.Text)
                'intReturnValue gets the result of the MsgBox function

 

   Then the value of 'intReturnValue' needs to be checked, and the appropriate string placed in the 'Text' property of the text box 'txtReturnValue'.  This can be done in a 'Select Case' statement.

        Select Case intReturnValue
        Case Is = vbOK         'if intReturnValue = vbOK then assign txtReturnValue the string "vbOK"
                txtReturnValue.Text = "vbOK"
        Case Is = vbCancel
                txtReturnValue.Text = "vbCancel"
        Case Is = vbAbort
                txtReturnValue.Text = "vbAbort"
        Case Is = vbRetry
                txtReturnValue.Text = "vbRetry"
        Case Is = vbIgnore
                txtReturnValue.Text = "vbIgnore"
        Case Is = vbYes
                txtReturnValue.Text = "vbYes"
        Case Is = vbNo
                txtReturnValue.Text = "vbNo"
        End Select

    Thus you get the following code (also shown above).

Private Sub cmdMessageBox_Click()
        Dim strText As String
        Dim strTitle As String
        Dim intReturnValue As VbMsgBoxResult
        Dim intButtons As VbMsgBoxStyle

        strText = txtText.Text
        strTitle = txtTitle.Text

        If optOK.Value = True Then
                intButtons = vbOKOnly
        ElseIf optOKCancel.Value = True Then
                intButtons = vbOKCancel
        ElseIf optRetryCancel.Value = True Then
                intButtons = vbRetryCancel
        ElseIf optAbortRetryIgnore.Value = True Then
                intButtons = vbAbortRetryIgnore
        ElseIf optYesNo.Value = True Then
                intButtons = vbYesNo
        Else
                intButtons = vbYesNoCancel
        End If

        intReturnValue = MsgBox(txtText.Text, intButtons, txtTitle.Text)

        Select Case intReturnValue
        Case Is = vbOK
                txtReturnValue.Text = "vbOK"
        Case Is = vbCancel
                txtReturnValue.Text = "vbCancel"
        Case Is = vbAbort
                txtReturnValue.Text = "vbAbort"
        Case Is = vbRetry
                txtReturnValue.Text = "vbRetry"
        Case Is = vbIgnore
                txtReturnValue.Text = "vbIgnore"
        Case Is = vbYes
                txtReturnValue.Text = "vbYes"
        Case Is = vbNo
                txtReturnValue.Text = "vbNo"
        End Select
End Sub