A Text Box Walkthrough - DOC by hcj

VIEWS: 12 PAGES: 7

									                                                                                           nd
Introduction to Computing (CS101)                                                     22        Sep, 04



                                    Lab 3 - Visual Basic

Visual Basic Events

What are events?
An event is what happens when you, for example, click on a button, drag the mouse, or double-
click on an object. There is a large list of events that could happen within your application; you
can choose the events that you want to respond to and then add programming code to tell VB
what to do when that event occurs.

In his Lab we will learn how to code events so that the interfaces we made in the previous lab can
be brought to some use. The visual basic statements to be executed when an event occurs are
written in a block of code called an event procedure. The structure of an event procedure is

Private Sub objectName_event ()
     Statements
End Sub

Let‟s try some examples to learn how to add events to our programs.

Exercise 1
- Create a new project and form
- Add a command button to the form




                                                                                                     1
                                                                                               nd
Introduction to Computing (CS101)                                                        22         Sep, 04



- Now double click the command button to go to the code window. This window looks like this




                                                                                          Procedure
        Object



The first drop down menu called Object points to the current Object for which code is being
written. The second drop down menu called Procedure points to the event for which the code will
be executed.
In this case that code you will write will be executed when you will click command button.

Copy the code written in bold to code window
          Private Sub Command1_Click()
               ‘This program will change the caption of the form
               ‘upon clicking the command button in the
               ‘execution state
               Form1.Caption=”Caption of the form has changed”

          End Sub

Note: The sentences written after the single apostrophe („) will be treated by visual basic as
documentation comments and it will not compile these. Starting a line with REM or Rem will also
have the same effect. It is very good programming practice to include documentation comments
and it will be expected of the students to include these comments to explain their code.
    -     Now to run the program press F5 or click on the run icon which is in the tool bar          .
    -     Click the command button and see the caption of the form changes.
    -     Then terminate the program by pressing Alt + F4 or clicking the End icon            in the tool
          bar

Exercise 2
Now we will learn how to add other events to program.
   - Start a new project and form
   - Add text box and command button to form.
In last lab we have changed properties of control through properties window. Today we will learn
how the same thing can be done through code.

We want to change text of the text button on clicking command button.



                                                                                                         2
                                                                                      nd
Introduction to Computing (CS101)                                                22        Sep, 04




    -   Click View Code on Project window to add code.

You will get following code window




    -   Select Text1 from object drop down and change the procedure(using the drop down
        menu) from Change to GotFocus. Then type the lines
        Private Sub Text1_GotFocus()
        Text1.Height = 2000
        Text1.Text = "Got focus"
        Text1.FontBold = True
        Text1.FontSize = 15
        End Sub




                                                                                                3
                                                                                          nd
Introduction to Computing (CS101)                                                    22        Sep, 04



    -   Next create an event procedure for the command button that should be performed when
        the command button is clicked. Type in the code

        Private Sub Command1_Click()
        Text1.Height = 1000
        Text1.Text = "Button Clicked"
        Text1.FontBold = False
        Text1.FontSize = 10
        End Sub

    -   Now run the program by pressing F5. Click the command button and then the text box to
        see how code works.

Problem Questions

Make the following interface, which contains 1 Label and 3 Command buttons

Program the command buttons so that when each is pressed the words on the label are
displayed with the stated alignment.
Also note that the command buttons are access keys.




First make an identical interface. Then code so that pressing the command buttons should alter
the background and foreground colors in the text box.




                                                                                                    4
                                                                             nd
Introduction to Computing (CS101)                                       22        Sep, 04



Using Text Boxes and Picture Boxes

Exercise 1

Make a form with 2 text boxes and four command buttons as follows




Now type in the following code in
the code window




Exercise 2
Make a new form insert a text box, a picture box and a command button




                                                                                       5
                                                                                               nd
Introduction to Computing (CS101)                                                         22        Sep, 04



Now type in the following code




Note: Dim var as String means that we are declaring a variable called „var‟ and it will store a
string in it. When we say var=Text1.Text we are storing the text written in Text1 in the string var.
then in the third line of the code we are displaying var.

Now Run the program and Type in any text in the Text Box and then click display
As you will see, whatever you type in text 1 will be displayed in the picture box. Text box is being
used as an input method and the picture box is used for output.

Problem Questions
Create a form with 2 text boxes, a picture box and a command button.




Code the program, so when you type text in text box 1 and then you click “Display”, the text that
you have typed should appear in both Text Box2 and Picture Box.




                                                                                                         6
                                                                                          nd
Introduction to Computing (CS101)                                                    22        Sep, 04



Using Option Buttons

Exercise 1

    -   Start new project
    -   Make following interface
    -   Change the names of Option buttons as Addition, Subtraction, Multiplication and Division.
    -   Change Names of text boxes as Number1, Number2 and Answer.




    -   Add click events on each of option buttons
    -   For example add following code, such that on clicking Addition Answer text box displays
        sum of numbers appearing in Number1 and Number2 Text boxes.

        Private Sub Addition_Click()
        Answer.Text = Val(Number1) + Val(Number2)
        End Sub
    -   Similarly add code for rest of option buttons

    Note: - (Subtaction), * (Multiplication) and / is used for Division.

Problem Question

Make the following interface. When one of the command buttons is pressed the face changes to a
Smiley face (Wingdings character “J”) or a Sad face (Wingdings character “L”). The face shown at
run time is made by Wingdings character “K”.




                                                                                                    7

								
To top