VB: Things You Should Know

Put Your Code In The Correct Event Procedure

Make sure that you put your code inside the correct event procedure. There are many events generated for each control, such as when you click on the control, type data into the control, move the focus to or away from the control, and more. Most of these events are ignored by our programs. If you want your program to respond to an event that occurs for a control, you need to put code inside the procedure that will be executed when that event occurs for that control.

The event procedure names are built from the name of the control followed by the name of the event:

     controlname_eventname

This event procedure will execute when the event eventname occurs for control controlname. For example, if you want to execute some code when the user clicks on a button, that code should be in the event procedure named

     buttonname_Click

where buttonname is the name of the button the user will click on to execute this code.

One way to start writing an event procedure is to double click on the control in the form. This will open the code window with an empty event procedure for that control. However, it may not be the event procedure you want to write. Another way to start writing an event procedure is to use the drop down lists at the top of the code window. The one on the left lets you choose a control. The one on the right lets you choose an event. Using these drop down lists gives you empty event procedures so you can write your code.

Get a Number From a TextBox

You must declare a variable to store the number. Use the Dim statement to declare the variable and choose type Integer or Single. Choose a meaningful name for the variable: one that relates to the value being stored.

     var = CInt(textboxname.Text) [if var is an Integer]
           or
     var = CDbl(textboxname.Text) [if var is an Double]

where var is the name of the variable which will store the value, and textboxname is the name of the TextBox whose value you want to get. The CInt function converts the data in the TextBox into integer form; the CDbl function converts the data in the TextBox into double form.

Get a String From a TextBox

You must declare a variable to store the string. Use the Dim statement to declare the variable and choose type String. Choose a meaningful name for the variable: one that relates to the value being stored.

     var = textboxname.Text

where var is the name of the variable which will store the value, and textboxname is the name of the TextBox whose value you want to get. No conversion function is needed, since the data in the TextBox is a String, and the variable you are storing it into is also a String.

Display a Number in a Label

To print in a Label, you need to change the Text property of the Label. When you are printing a number, the number needs to be converted from numeric format to String format. You can use the CStr function or you can use one of the Format functions.

     labelname.Text = CStr(num)

where num is a numeric variable or constant which will be displayed in the label. To format the number use the FormatNumber, FormatCurrency or FormatPercent function instead of the CStr function.

Display a String in a Label

To print in a Label, you need to change the Text property of the Label.

     labelname.Text = string

where string is a String variable or constant. Remember that a string constant appears in double quotes, such as "this is a string constant".

Display Output in a ListBox

To print in a ListBox, you must add to the list of items contained in the ListBox. To do this, you must use the Add function. An item can be a number or a String. Each time you call the Add function it will add another line to the ListBox. For example:

     listname.Items.Add(num)
     listname.Items.Add("here is the second item")

will add two lines to the ListBox named listname). The first line is the value of the variable num, and the second line contains the string "here is the second item". If you want to clear out a list box, use the function Clear:

     listname.Items.Clear()


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2007