Visual Basic


Home Introduction

Lesson 5- Building a Program, Pt 2


Step 1:


Double click on the button. You'll notice that it opens up the code.


Step 2:


Under Public Class Form1 we will declare some variables. Type Dim then, let's add some variables. For the sake of learning, copy what I have. You'll understand where they come into play in just a sec. Set them As Single . (Warning!: Depending on what candy you have, skit, kids, and whop will be different. Name the variables depending on your candies.)


Step 3:


Under Private Sub Button1_Click we want to add the rest of the code. Here is where those variables that we just set come in. When it's time to do totals, we want our program to know how much everything costs.

Notice how the first line is ticket1 = txtadtick.Text * 7.0. ticket1 represents our adult tickets, and txtadtick is the name I set my adult ticket text box to. It is $7.00 so that is why I multiplied by 7. This way when we get down to calculating our totals, the program knows how much our adult tickets cost.


Step 4:


Let's take care of those check boxes. The code is pretty simple, actually.

What it's saying basically is, "if the check box is checked, then the value equals (price goes here)." Else, (if it's not checked) it equals nothing. Use the names that you set the check boxes to. Now, since I have skittles for $1.50, I wrote the code accordingly. Depending on what candy and prices you have, you will need to make yours different.


Step 5:


In order to do the radio buttons, double click them.

Then it's easy. Just set their values using the variable we declared! So for example, if you double click the radio button for the small popcorn, it equals 3.5 .


Step 6:


Let's find our totals.

You remember the names you set the labels next to the totals? Here is where they come into play.


Step 7:


That's it! Go test your work. If there's errors, Visual Basic will tell you where they are.

In the end, this is what your code should resemble. (WARNING!: Do you see the line that says PicBox1.Visible = True ? You won't need that. Only type that if you happen to put a picture on your form.)