Greeting
Up

Greeting Program

By making this program you will learn about:

bulletlabel controls
bullettextbox controls
bulletbutton controls
bulletthe messagebox statement (MsgBox)
bulletthe inputbox function
bulletthe assignment operator (=)

A three minute animation is available to introduce this lesson. If (and only if) you are on the European School 3 Brussels internal network then you can view an animation via the local network by following one the links on the left below. If you are located outside European School 3 then please follow one of the links on the right (these are large files so you must have broadband):

  in school, from server outside school, via internet
Greeting project3 (3 minutes, 8.5MB) coming! coming! coming! coming!

Start Visual Basic 2008 Express Edition, select Windows Application and give the name Greeting (if VB is already running then choose File>New Project). Save the project immediately with File>Save All (not Ctrl-S, for that only saves the form file). Give the name Greeting, the location H:\VB if you are at EEB3 (otherwise use the default location), give the solution name Greeting and leave the box checked for the automatic creation of a folder with that name to hold your project. ALWAYS set the location to H:\VB if you are saving a project at school.  Don't forget to save your work every ten minutes or so - you can use Ctrl-S for this.

Make the form look like the one below by adding a label, a textbox and a button. Set the Name property of each control to the names indicated below (lblPrompt etc). Even though changing the name of a control does not change its appearance, the name is very important since it will be used by the program's code (instructions) to identify that specific control. Also, the 3 letter prefix conventionally placed at the beginning of names helps you to remember what type of control it is. For example, if the name begins 'txt' then you know it belongs to a textbox control.

I have set the BackColor property of the label to white so that you can see its edges more clearly - you can leave it gray if you like. I have set the font property of every control to 12 since size 8 is rather too small to be easily legible.

Once you have set the name and text property of each control (as well as the text property of the form itself) then we can write code so that this program inputs the user's name and then greets the user and tells the time. Double-click the button so that the code window opens - you should see something like this:

In Visual Basic terminology our Windows form is in fact a 'Class' (we'll come back to this important word much later) and VB has automatically marked the beginning and end of the class code with two 'wrapper lines'. These wrapper lines will be in every project that we make - just ignore them for the time being. Don't delete them and don't put anything outside these two class wrapper lines.

Between the class wrapper lines we see a subroutine or 'sub'. A subroutine is a kind of procedure, a set of instructions that perform some function. Like the class, the subroutine has automatically been given two wrapper lines. The first wrapper line is actually so long that you probably will not be able to read the whole line without scrolling - my picture only shows part of that line. We will ignore most of that line for the time being, except for the part that says btnGreet_Click. This tells us that the code in this subroutine will be run when the button called btnGreet is clicked. All the code that is to be run when that 'event' takes place must be placed between the wrapper lines of the btnGreet_Click subroutine (the class can contain many subroutines).

The tiny minus signs in squares to the left of the code allow you to hide the corresponding class or subroutine so that you can focus your attention on one part of the code. After collapsing a block of code like this you can unhide or expand it again by clicking the plus sign.

Type (or copy and paste) the following line between the wrapper lines of the btnGreet_Click subroutine:

    MsgBox ("Hello")

Try running the program by clicking the Start Debugging button in the toolbar at the top of the VB window or simply press the F5 key. Press the 'OK' button - the program should display the word 'Hello' in a message box. Dismiss the message box and stop the program by pressing the Stop Debugging button or by by pressing the close box in the corner of your form.

If your program does not work then you will need to 'debug' it. The most common error message is 'Handles clause requires a ....' - this means that the control names referred to in your code do not match the names you gave the controls using the properties window. Either you have made a mistake when you wrote the code or possibly when you set (or forgot to set!) the name property of each control. Check that you have named each control correctly, and that you have made no spelling mistakes in the code.

Now modify the line you typed or pasted previously so that it looks like this:

    MsgBox ("Hello, " & txtName.Text)

Test the new code - press F5 then type your first name in the textbox then press the 'OK' button. You should see a message box displaying something like 'Hello, Jack' (or whatever name you typed in the text box). In the above code, the ampersand character '&' is used to join together the text string "Hello, " and the user's name, which is found in the text property of the textbox. The process of joining bits of text together is called 'concatenation', so the '&' character is the 'concatenation character' in Visual Basic. Whenever you use the concatenation character '&' in your code, make sure you put a space on both sides of it. Note how the text property of the textbox is referred to using 'dot notation' in the form controlname.property - this is very standard in Visual Basic.

Now modify the code between the wrapper lines until it looks like this:

    MsgBox ("Hello, " & txtName.Text & ". The time is " & TimeOfDay)

Try running the program - the computer should now tell you the exact time according to its internal clock. Try replacing the word TimeOfDay at the end of the code with Now - what does the program do now? Replace Now with Today and run the program again - what happens?

Using a textbox is only one way of getting input from the user of your program - another neat way is to use an inputbox. An inputbox is rather like a messagebox except that it allows the user to type into it. An inputbox is an example of a 'function' because it 'returns' (sends back) something to the program - in this case it returns the text that was typed by the user. We will store the text returned by the inputbox into a 'variable' - a variable is like a box or container. In our projects we will use a general-purpose variable type called an 'Object' variable type . Object type variables can contain text (a block of text is called a 'string'), dates or numbers. Before you can use a variable in your code VB normally requires you to 'declare' (create) the variable but it is possible to suppress this requirement by adding the line Option Explicit Off at the very top of the code, above even the class wrapper. To keep this project as simple as possible, add that line now.

The modify the button code like this:

    country = InputBox("In which country were you born?")
    MsgBox ("Hello, " & txtName.Text & " from " & country)

Your complete code should now look like this (remember there is one line that is so long you cannot read all of it in my pictures):

Run the program and see what happens. It's very important to understand how the above code works, for the meaning of the '=' character is not at all what you would expect. You have seen this character in your math classes, so you think it is an equals sign. WRONG!! In Visual Basic the '=' operator does not usually work like an equals sign, instead it is an 'assignment operator' that takes or calculates what is on the right hand side and then stores the answer in the variable or property named on the left hand side. So a line such as

x = 3 + 6 

would evaluate the right hand side to equal 9 then store that number into the variable called x. In English, you could read the above line as 

'Set x to equal the result of 3 + 6'

You can see that a line like this

3 + 6 = x

which would be perfectly OK in a math class, would not be OK in Visual Basic because VB would first look for a variable called x, which it would not find, and then would try to store that 'nothingness' into '3 + 6'. This would crash the program because '3 + 6'  is neither a variable nor a property, so VB cannot store anything there.

In our project, then, the text returned by the inputbox function is stored into a variable called country and then, in the next line, the word "from" and the contents of the country variable are added after the user's name, in the message box. Note that the word country does not have quotes around it in this line, so the program assumes that this refers to the contents of a variable. Just to check that you understand this, change the btnGreet_Click code as shown below (note the extra quotes):

    country = InputBox("In which country were you born?")
    MsgBox ("Hello, " & txtName.Text & " from " & "country")

Run the program, and then undo the last change to the code before saving your work.

Congratulations! You have just made a 'real' VB program, tested it and run it successfully, all in less than an hour(?). I consider this to be a 'real' program because it has the three major features of almost all programs:

bulletInput - you entered your name and country into the program.
bulletProcessing - the computer joined your name to other text
bulletOutput - the computer displayed the results of the processing

Up Next