Lesson Ten, Part 2,Introduction to Windows Programming

If you closed the program after the last lesson you can open it back up by Starting Visual Basic Express and looking in the "Recent Projects" section and clicking on your dog_walking_project solution.

Your  next step is to add some code.  You can hide the toolbox by click the small push pin in the top right corner of the pane. 

We want to add code that will run when someone clicks on the button.  That is the event, associated with that object (why we call this event driven, object oriented programming).   You need to double click on the button and then the code window will open.

The code that is already their is this:

Public Class frmdogwalking

Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click

End Sub

End Class

The public class is the form, the private sub is the code that will run when the button is pushed (handles btncalculate.Click).  If you have any other private Sub lines it's because you double clicked on an object.  You can just leave it alone or delete them.  I recommend leaving it alone because if you delete the wrong code you will have problems.  So, let's add some code

First, place your insertion point (using the curser keys) in front of the P in Public Class and press enter.  We need to add some code above the prewritten code.  The code we want to add is:  Option Explicit On  You notice that when you start typing drop down boxes start giving you options to select from to place the code on the page.  This is called Intelasense.  The program tries to guess what you are trying to do and give you options.  Take advantage of the help whenever you can because it cuts down on the errors you can make.

Then next thing we want to do is add three places of memory (weekendrate, weekdayrate, and rate.  We are adding them so that our code is more understandable to others and easier to maintain.  Underneath the line Private Sub  add the following:

Dim weekendrate, weekdayrate, rate
weekendrate = 10
weekdayrate = 5

Those three lines declared the variables and then initialize them with values.  In the next lines of code we will be using the key word "me" to represent the form.  When you start with me. then intelasense will give you a list of objects, functions and subroutines associated with the form.  For us, we will be using it to select form objects (labels, textboxes, buttons, etc).  For the next line of code, after typing me you will select the combo box object, then select the text property.  The next lines of codes will be our decision of which rate to use:

If Me.cbodaytype.Text = "weekend" Then
    rate = weekendrate
Else
    rate = weekdayrate
End If

Me.lbltotalcost.Text = FormatCurrency(Me.txtnumdogs.Text * Me.txtnumhours.Text * rate)

Once you have typed the code, press the Save All button, then the > debug button to test out the form by typing 1 dog, 1 hour and weekend.  The amount $10.00 should display.

Your final code should look like this:

Option Explicit On

Public Class frmdogwalking
    Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
        Dim weekendrate, weekdayrate, rate
        weekendrate = 10
        weekdayrate = 5

        If Me.cbodaytype.Text = "weekend" Then
            rate = weekendrate
        Else
            rate = weekdayrate
        End If

        Me.lbltotalcost.Text = FormatCurrency(Me.txtnumdogs.Text * Me.txtnumhours.Text * rate)

    End Sub   

End Class