next up previous
Next: 3. Some mathematics (and Up: VisualBasic Previous: 1. An Overview of

2. Your first VisualBasic Project (and the Print Statement)

To begin designing your first VisualBasic Project, you must first open the VisualBasic environment. If there is no short-cut icon on your desktop, then you should be able to open the VisualBasic Programming Environment by going to the Start button (at the bottom left of your screen) and then selecting Programmes where you should see the VisualBasic 6.0 item. Once you select this, the VisualBasic Programming window will open. You will see a window with a Standard EXE icon. Double click on this. (In this course, we will only ever use the Standard EXE template. You'll have to buy a VisualBasic book if you want to know about the other templates!!)

After you have double clicked on the Standard EXE icon, you will see a blank, grey Form.

Have a look at the left-hand-side of the VisualBasic Programming window. You will see a number of icons. Move your mouse over the grey rectangle. You will see the text CommandButton appear. Double click on the icon. You should see a grey rectangle appear in the middle of the form called Command1.

You have just inserted your first button into your form!

Double click on this button. You will see the following:

Private Sub Command1_Click()

End Sub

You have now left the Visual (or Form) component of the VisualBasic Programming environment and are now in the Basic (or code) component. The code which you are about to enter is associated with the button called Command1 on your form. Type the following line:

Print "Hello Swansea"

Your screen should now be like this:

Private Sub Command1_Click()
Print "Hello Swansea"
End Sub

(Note that if you type print "Hello Swansea" with a lower case "p", then once you move the cursor away from that line, the "p" becomes upper case. This is VisualBasic showing off and making everything look tidy.)

We are now ready to run this VisualBasic project. To do this, just click on the icon near the top of the screen which is like a play button on a video player (i.e. like a triangle pointing to the right).

As if by magic, a copy of your form appears on the screen. Click on the button called Command1. You will see the words "Hello Swansea" appear at the top of your form! To exit this "run mode", click on the icon in the shape of a square near the play button and you will return to play mode.

To stop the programme from running, you can click on the X on the top right of the window (as usual) OR you can click on the square button which looks like a stop button on a video player. (It's next to the "play" button!)

If you are stuck doing this, then you can download a working version very similar to this here.

You can print more than one thing on the same line using either a comma (which plays the role of a TAB character) or semi-colon (which leaves no space at all).

Edit the programm above so that it now looks like:

Private Sub Command1_Click()
Print "Hello Swansea","and the rest of Wales"
Print "Hello Swansea";"and the rest of Wales"
End Sub

When you run this programme you will see the difference between the comma and semi-colon. Note that you can put comma's and semi-colon's at the end of the line in a Print statement too. The following code would do the same as the code above.

Private Sub Command1_Click()
Print "Hello Swansea",
Print "and the rest of Wales"
Print "Hello Swansea";
Print "and the rest of Wales"
End Sub

CHECKPOINT: Take the example programme from here and modify it so that it prints out the messages all on one line. Change it to print out messages of your choice!

VisualBasic operates in two modes. The first is the editting/designing mode (which is what you are doing when you are creating and modifying your form and the associated code), and the second is the running mode (which is what happens once you click on the play button). You can get back to the edit mode by clicking on the stop button.

You can add a CommandButton to a form by double clicking on the corresponding icon in the Toolbox.

The Print statement is used to output to the screen.


next up previous
Next: 3. Some mathematics (and Up: VisualBasic Previous: 1. An Overview of
Chris Allton 2006-10-27