Skip to content

Visual Basic Tutorial "Writing the Codes".

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #190494
    richous05
    Member

    Each control or object in VB can usually run many kinds of events or procedures; these events are listed in the dropdown list in the code window that is displayed when you double-click on an object and click on the procedures’ box(refer to Figure 2.3). Among the events are loading a form, clicking of a command button, pressing a key on the keyboard or dragging an object and etc. For each event, you need to write an event procedure so that an action or a series of actions can be performed.

    To start writing an event procedure, you need to double-click an object. For example, if you want to write an event procedure when a user clicks a command button, you double-click on the command button and an event procedure will appear as shown in Figure 2.1. It takes the following format:

    Private Sub Command1_Click
    (Key in your program code here)

    End Sub
    You then need to key-in the procedure in the space between Private Sub Command1_Click…………. End Sub. Sub actually stands for sub procedure that made up a part of all the procedures in a program. The program code is made up of a number of statements that set certain properties or trigger some actions. The syntax of Visual Basic’s program code is almost like the normal English language though not exactly the same, so it is very easy to learn.

    The syntax to set the property of an object or to pass certain value to it is :

    Object.Property
    where Object and Property is separated by a period (or dot). For example, the statement Form1.Show means to show the form with the name Form1, Iabel1.Visible=true means label1 is set to be visible, Text1.text=”VB” is to assign the text VB to the text box with the name Text1, Text2.text=100 is to pass a value of 100 to the text box with the name text2, Timer1.Enabled=False is to disable the timer with the name Timer1 and so on. Let’s examine a few examples below:

    Example 4.1
    Private Sub Command1_click

    Label1.Visible=false

    Label2.Visible=True

    Text1.Text=”You are correct!”

    End sub

    Example 4.2
    Private Sub Command1_click

    Label1.Caption=” Welcome”

    Image1.visible=true

    End sub

    Example 4.3
    Private Sub Command1_click

    Pictuire1.Show=true

    Timer1.Enabled=True

    Lable1.Caption=”Start Counting

    End sub

    In example 4.1, clicking on the command button will make label1 become invisible and label2 become visible; and the text” You are correct” will appear in TextBox1. In example 4.2, clicking on the command button will make the caption label1 change to “Welcome” and Image1 will become visible. Example, clicking on the command button will make Picture1 show up, timer starts running and the caption of label1 change to “Start Counting”.

    Syntaxes that do not involve setting of properties are also English-like, some of the commands are Print, If…Then….Else….End If, For…Next, Select Case…..End Select , End and Exit Sub. For example, Print “ Visual Basic” is to display the text Visual Basic on screen and End is to end the program. Other commands will be explained in details in the coming lessons.

    Program codes that involve calculations is very easy to write, you need to write them almost liket what you do in mathematics. However, in order to write an event procedure that involves calculations, you need to know the basic arithmetic operators in VB as they are not exactly the same as the normal operators we use, except for + and – . For multiplication, we use *, for division we use /, for raising a number x to the power of n, we use x ^n and for square root, we use Sqr(x). More advanced mathematical functions such as Sin, Cos, Tan , Log and etc. There are also two important functions that are related to arithmetic operations, i.e. the functions Val and Str$ where Val is to convert text entered into a textbox to numerical value and Str$ is to display a numerical value in a textbox as a string (text). While the function Str$ is as important as VB can display a numeric values as string implicitly, failure to use Val will results in wrong calculation. Let’s examine example 4.4 and example 4.5.

    Example 4.4
    Private Sub Form_Activate()

    Text3.text=text1.text+text2.text

    End Sub

    Example 4.5

    Private Sub Form_Activate()

    Text3.text=val(text1.text)+val(text2.text)

    End Sub

    When you run the program in example 4.4 and enter 12 in textbox1 and 3 in textbox2 will give you a result of 123, which is wrong. It is because VB treat the numbers as string and so it just joins up the two strings. On the other hand, running exampled 4.5 will give you the correct result, i.e., 15.

    We shall now look at more examples in mathematics.

    Now we shall attempt to write the codes for the cylinder program where the interface is shown in Figure 2.8. First of all, name the textbox as radius, hght, and volume. To get the values of the various textboxes, use Val(radius.text), Val(hght.Text) and assign them to the variables r and h. In addition, assign the value 22/7 to the variable pi. After that, write the equation v = pi * (r ^ 2) * h to compute the value of volume of cylinder and then assign it to the variable v. Finally, display the value in the volume textbox using the function Str$.

    Private Sub OK_Click( )

    r = Val(radius.Text)

    h = Val(hght.Text)

    pi = 22 / 7

    v = pi * (r ^ 2) * h

    volume.Text= Str$(v)

    End Sub

    When you run the program, you should be able to see the interface as shown in Figure 2.8. If you enter a value each in the radius box and the height box, then click OK; the value of the Volume will be displayed in the volume box.

    I shall attempt to explain the above source program to newcomers in Visual Basic ( If you are a veteran, you can skip this part) . The program could be explained using pseudo codes as follows:

    Procedure for clicking the OK button to calculate the volume of cylinder

    get the value of r from the radius text box

    get the value of h from the height text box

    assign a constant value 22/7 to pi

    calculate the volume using formula

    output the results to the Volume text box

    End of Procedure

    Borrowed From
    EnJoY

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.