Mr. Emu Tutorials: Lesson 1 - The Basics *VB6*

Lenny

Press "X" to admire hat
Joined
Jan 11, 2007
Messages
3,958
Location
Manchester
Before I start anything, I'd like to direct attention to the What You Need thread. If you want to learn VB6 then you must use a VB6 program, and it must be on a Windows PC.

Anyway, this lesson is going to be the first of about twenty. In it, you'll learn quickly how to use the program, and we'll start coding.

If you're having problems, please post the code with your plea for help. I don't know how proficient other Chronites are in VB6, so for the moment expect only me to reply with answers - which may not be until the next day, if the question is posted later than I'm on until.

I'm going to put explanations of technical terms in a different font and colour.

----------

The Basics

Visual Basic 6 is an event-driven, third-generation programming language.

Event-driven - a program in which the flow is determined by inputs by the user or from sensors. For example, a mouse button click, the press of a keyboard, or a temperature reading.

Third-generation - the first generation of programming languages is Binary, code that is programmed entirely in 1's and 0's. The second generation, Assembly Language (or Machine Code) makes use of three or four letter mnemonics, one line of which translates directly into one line of binary code. A third-generation language, such as Visual Basic, uses proper words, one line of which can translate into multiple lines of Assembly Language. Third-generation languages are much easier to learn than second- or first-generation as the user can read through the code and be able to understand what it does.

Programs in Visual Basic 6, and other event-driven languages, are built using one or more forms, on which are placed controls. Code can be attached to both the form and the controls.

To new users, the initial layout of the program can be confusing, so I've uploaded a picture to make navigation easier (if you can't see it because Photobucket may be blocked where you are, PM me and I'll send you the picture).

The Toolbox down the left-hand side of the screen contains the most common controls. More can be added, but I'll get into that in a different lesson. Again, here's a picture to help with navigation. The terms in brackets are the technical terms given to the controls by Visual Basic.

Down the right-hand side of the screen you have the Project Explorer, which displays any forms and modules (again, I'll get to these at a later date) you have in your project, and below it the Properties Window. Below that is also the Form Layout Window, but I never use that so I've closed it. Back to the Properties Window - when you place a control from the toolbox onto a form, is it an example of an object. The form itself is also an object. All objects have properties (a descriptive feature of an object), which show up in the Properties Window. I'll discuss them when they're needed.

With that done, we're ready to make our first program! Each program will be labelled in the following way:

Program [Lesson Number][Program Letter]- [Program Name]

Thus, the third program in the eighth chapter, which is, "A Program containing Code" will be labelled Program 8c - A Program containing Code. I advise that all programs be saved using the number and letter. So the above would be saved as Program 8c or just 8c.

-----

Program 1a - Displaying Text

This first program is simply going to display everyone's favourite phrase, "Hello World!", when the program loads.

1. First of all, as always, run Visual Basic 6 and create a new Standard EXE.

2. When the form window shows up, click the form (it should be covered in dots), and change the caption property in the Properties window to "Displaying Text". Scroll down through the properties until you find the Font property, and change it to anything you like - why not something big and bold?

3. Double-click anywhere on the form to open the code template. Whenever you double click the form or an object, this window will appear.

4. Between the two lines of code already there, enter another two lines, so that you have these four altogether:

Code:
Private Sub Form_Load()
    Form1.Show
    Form1.Print "Hello, World!"
End Sub

If you've changed the name property of the form, then replace "Form1" with the name you gave the form.

5. Press F5 to run your program and marvel at your genius. You can also press Ctrl + F5 to run the program - this method checks the code for errors before it runs.

Notice how the two lines you inserted (Form1.Show/Form1.Print "Hello, World") are indented? Whilst not imperative, it's good practice to indent your code. Not only does it make it look neater, but it allows you to read through it easier.

6. Save the program.

I'll explain what happens when the program is run.

Code:
Private Sub Form_Load()
    Form1.Show
    Form1.Print "Hello, World!"
End Sub

When the form loads
Show the form
Print the text "Hello, World!" to the form
End.

.Show and .Print are what are known as methods. All objects have methods - actions you can perform on an object. Note how an object and its method are separated by a dot.

Before we go on, try the above program but with different text between the quotation marks after Form1.Print.

-----

Object Naming Conventions

To make programming in VB6 even easier, there are a set of widely used naming conventions for objects. These are lower case three-character prefixes which are added to the name given to a control. For example, a text box that holds a name might be called txtName. The prefixes make objects in code easily recognisable, and allows you to instantly guess their purpose and name.

You must remember that object names must not contain any spaces. Instead, a capital letter for each new word in the name helps split the name into individual words.

Here's a list of prefixes for the most common controls you'll use (most of which are contained in the toolbox by default):

VBPrefixes.jpg


-----

Program 1b - Change a Message

1. Make a new project, then change the caption properties of Form1 to "Change a Message".

2. Choose the Label control from the toolbox and, with the left mouse button, draw a rectangle in the upper half of the form window. Click the Command button control and draw two buttons below the label.

Lesson1-1alayout.jpg


3. Click the label and change its name property to lblMessage. Delete "Label1" from the caption property to leave it blank. You can change the border property to 1 - Fixed Single to make it look like mine.

4. Change the name of the left command button to cmdGoodMessage and its caption to Give me a Good Message (or something to that effect).

5. Do the same to the command button on the right, but with the name cmdBadMessage and the caption Give me a Bad Message.

6. Double click the "Good message" command button and add a line of code so that it looks like this:

Code:
Private Sub cmdGoodMessage_Click()
    lblMessage.Caption = "Lenny is a GOD!"
End Sub

You can put any text within the quotation marks,

7. Double click the "Bad message" command button and add a similar line of code, but this time with a bad message:

Code:
Private Sub cmdBadMessage_Click()
    lblMessage.Caption = "Lenny is only a Demi-God."
End Sub

You'll notice that we didn't use the "=" sign before text in the first program. The whole line of code is what is known as an assignment statement., and basically means that the text inside the quotation marks after the sign is to be stored or assigned to the caption property of the label. It was not needed in the first program as the text was printed, rather than assigned to a form property.

8. Save the program and run it. Click your buttons to change the message in the label.

And that's your second program! Feels good, doesn't it?

-----

Extra Exercises

These exercises will hopefully allow you to build upon the skills learnt in this tutorial, and cement the lesson in your mind.

1. Change Program 1a to display your name rather than "Hello, World", and add a line of code to display your address after your name.

2. Write a new program that changes the Title (name) of the form when a command button is clicked. You'll need to look through the other methods that objects have. When you insert the dot to add a method to an object, notice how a drop-down menu of methods appears. I wonder what you can find in there.

3. Put two command buttons on a form. Set their captions to Show and Hide. When the Show button is clicked, a label should appear with a message in its caption. When the Hide button is clicked, the label should disappear, as if the label is no longer visible (one would falsely assume that an invisible label is visible).

4. Write a program so that words typed into a text box change colour to red or blue when an associated command button is clicked. Think about a property of the text box that will allow word wrapping, and think about assigning vbRed and vbBlue, as appropriate, to the property of the text box that determines the colour of its contents.

And generally play around - see if you can change the colour of the form when a button is clicked, see if you can print text to the form when a button is clicked. Examine as many methods as you want.

----------

And that's that. As usual, if you have a problem, please post the code (in CODE tags this time) along with your problem.

If you make a program that you're extremely proud of and want to show others, then add the files to a .zip file (you should have three files - ProgamName.vbw, ProgamName.vbp and FormName.frm) and attach it to a post or upload the .zip file to a file host (such as Game Downloads, Game Patches - FileFront.com - you'll need to make a free account).

Happy Coding!
 
Any thoughts about VB from anyone, yet?

If anyone is thinking that it's actually going to be years before they get anywhere, then to get the hype train a-steaming away, methinks I'll post an example of what I've been doing recently - a full, from scratch, calendar system. And let me tell you - I've used a lot of subs, numerous .Show's and I've changed Powers know how many captions, which just goes to show that even the absolute basics are used in complex programming (yes, yes, I'll own up, I'm blowing my own Trumpet a bit here... but really, it is quite complex. Not in a programming sense, but the fact that the logic behind it all, and the thought process to work out how to do it, were both mind-bending!).
 
I haven't played with it yet... had a weekend that wasn't conducive to monkeying with code... *g*
 
I've decided to move the VB tutorials to Fridays - I just don't have the time to do what I do on Wednesday evenings. I can just bout fit the HTML tutorials in on Mondays, but they're a lot shorter.

Hopefully it won't clash with Leisha's next Flash tutorial. If it does, then it will only be for a couple of weeks.

Sorry if anyone was really looking forward to it today. You can wait another two days though, surely? :p
 
Oh, Lenny, I think we can manage another day or two. ;)

Especially those of us what haven't gotten to the actual first lesson yet *sheepish*
 

Back
Top