Your First Object

Objects are the basic things that you use to create visual programs in VB. They are things like buttons, pictures, listboxes etc. In this tutorial you will learn how to create them and edit their properties.

Creating a project

When you load Visual Basic you should get a screen similar to this. You may have more options or a slightly different layout as this is taken from Visual Basic 5.0 .

Startup Screen

Startup screen detail

Select 'Standard EXE' and click Open.

Inserting an object

Now you have created a project we are going to insert a button onto our form (the blank window currently being displayed). First bring up the toolbox by clicking on its icon on the menu bar at the top.

Getting the toolbox

In the toolbox find the button icon and double click it.

toolbox

Your form should now look like this

Form with button

Renaming your button

Bring up the properties by clicking on the properties button next to the toolbox button.

Properties

Make sure that you have the button selected (click on it) and then look at the properties box.

Button's properties

Now we shall rename our button. click in the name box and type 'Button'. It is good to name your objects with relevant names so that they are easy to remember and anybody else looking at your code will find it easier. Another thing that is a good idea is to use capital letters, this really helps when you have two word combined eg. SaveButton. Names cannot start with numbers or symbols and cannot contain spaces.

Now we shall change the caption of our button so the user of our program knows what to do. Find the caption field and change it to 'Click Me'. You will notice that the words appear on the button in the form.

Coding

This is the scary part, writing the code for your program. VB has a nice syntax which looks quite user friendly so its not too bad. Coding is easier than you think and you should pick it up quite quickly.

To get to the code double click the command button that you have created. You will see the code window appear and your nice graphical box disappear.

Code

code detail

Now type inside the lines of text 'button.', a list box will appear with lots of options. Scroll down to 'Caption' (or start typing caption and it will be highlighted) click on 'Caption' and then type '="Hello World"'. This is your first piece of code.

In terms of how it works, you typed in the objects name which was button to tell visual basic you want to use that object. You then typed what property you wanted to change by typing .caption (note that you don't need to select it from the box it can just be typed). After this you said that this property should equal "hello world". All strings must be put inside speech marks to tell VB that it is a string.

Testing it

Now click the run button (the arrow on the top toolbar) or press F5. You should see your form appear. If it doesn't check that you have typed everything correctly. Notice that the dots have gone from the form.

Running

Now click the button.

After you click

Thats your first program complete and working. You can save it if you want or just discard it as we won't be using it again.

© Jonathan Waller 2005; QuantumState Visual Basic