User Input

This tutorial will look at handling the input that the user gives us. We will look at several methods of retrieving user input.

First create a new project like in the last tutorial. Bring up the toolbox and properties if the are not already there. Insert a command button by clicking on the icon in the toolbox and then clicking and dragging on the form.

inserting a button

Change the button caption to 'Say Hello'.

Form with button

Insert a textbox and place it just above the button. You insert it the same way a a Command Button. To find out which items in the toolbox are which just hover your mouse aver them and wait for the tooltip to appear. Insert a label to the left of textbox but quite close as this will tell the user what to put in the textbox. Change the text property of the textbox to nothing so it appears blank. Change the caption property of the label to 'Name:'.

Complete interface

This is the graphical part of our project done, so now we need to move onto the code.

The Code

First we need to detect when the command button is clicked. VB does this for us so all we need to do is double click the button which brings up the code window. You will notice the cursor is inside these two lines of code:
Private Sub Command1_Click()

End Sub
Basically a 'Sub' is a small section of code that will be executed (run) at a certain point in the program. 'Command1_Click' tells us that the code should be run when Command1 is clicked. 'Private' and '()' do not really matter for now.]

Now we will display a message saying hello.
call MsgBox("Hello " & Text1.Text, vbOKOnly, "A Message")
MsgBox is a function which displays a message in a little box (like things that pop up asking if you want to save your work). We call this function using the 'Call' command. In the brackets we enter data which we want to send to the function. The first piece of data we send is the main content of our message box. First we have "Hello " (note the space) then we use the '&' operator (operators do small thing like the addition operator is '+' etc.) which combines two strings. The second part of the text is the information from the textbox. We just put the name and the property which contain the text. Note that '.Text' is a rad and write property so you can set it and read the data from it. After you typed the comma you should have seen a dropdown menu come up with loads of options. These just change the look of the message box such as what icon is next to it and the number of buttons etc. Finally we entered the title of the message box.

Now run your program and click the button. You can keep or discard this project as we will be using some of the same parts in the next tutorial (Variables). I usually discard projects as it is very quite and easy to build an interface and it is a bother saving and finding projects. I would recommend creating a new folder for each project and keeping all of the files in the one folder.

© Jonathan Waller 2005; QuantumState Visual Basic