Visual Basic Tutorial, Page 4
Let's jump straight into creating your own little application!
To the left of your screen you should see a lot of little icons under the heading 'General'. These are called components - or if you want to be really geeky, ActiveX controls. Basically, they're "things" you can add to your program (technical description, that) - such as a button or picture. Let's see one in action...
So what are these 'things' I can add to my program?
- Double-click on the component represented by a black 'A'
An item called 'Label1' should appear in the centre of 'Form1'.
This is an example of a component - albeit not very impressive for something that holds the title of "ActiveX control". But don't worry, it gets better. Trust me, I'm a programmer. Oh, I've done that joke haven't I?
To the bottom-right of your screen you should have a box entitled "Properties - Label1". If it doesn't say 'Label1', click on the label in the middle of your form.
The items you see here are "properties" of the label. They tell it how to work - what text to display, what colour it should be, whether it should have a border, and so on. You can easily change these by typing in or selecting new values from the list.
Let's try it now:
- Click on the box to the right of the 'Caption' property
- Replace the existing "Label1" value with "My First Wizzy Program"
- Press Enter
Done that? Well done, you've just changed your first property! Did you notice how the label altered too?
- Click on the label and hold your mouse down
- Drag your mouse to the upper left hand corner of "Form1" and release the mouse button
The label should have moved. Smashing! If you're still following me, I'm really pleased - you're obviously a natural born VB'er. In fact, you should be writing this article.
Your screen should look something like this right now...
Let's go one step further. Double click on the button in the "General" toolbar. This is a Command Button - you'll find them in virtually all Windows programs, usually in the form of "OK", "Apply" or "Cancel" buttons.
Let's play around with this button...
- Change its Caption property to "Click Me!"
- Move down in the Properties list and change the Font to something wacky!
- Resize the Command Button using the eight blue squares surrounding it
My Form looks like this right now...
Finally, let's add our first line of code:
- Double click on the Command Button
That should bring up a pretty boring looking screen. This is where you "code", the most popular pastime among geeks and geekesses worldwide.
Your screen should look something like this...
Private Sub Command1_Click()
<Cursor flashing here>
If it does, that's brilliant!
- Now type in...
MsgBox "Welcome to my wizzy program!"
I'll explain what we're doing here. The "Private Sub Command1_Click()" tells Visual Basic to run this line of code when someone clicks on the Command Button called Command1. When they do, the line of code we tapped in runs - telling Visual Basic to display the designated sentence in a message box (MsgBox). The "End Sub" merely tells VB to stop running the code.
Simple as that.