Zelaron Gaming Forum  
Stats Arcade Portal Forum FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read
Go Back   Zelaron Gaming Forum > The Zelaron Nexus > Science and Art

 
 
Thread Tools Display Modes

 
Mr. Emu Tutorials: Lesson 1 - The Basics *VB6*
Reply
Posted 2008-07-11, 08:34 AM
Before I start anything, I'd like to direct attention to the Tutorials 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 others 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
.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):



-----

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.



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 (remember - 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) 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!
Old
Profile PM WWW Search
Lenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basics
 
 
Lenny
 



 
Reply
Posted 2008-07-11, 10:19 AM in reply to Lenny's post "Mr. Emu Tutorials: Lesson 1 - The..."
thanx lenny
Tim
I know you
said not to
deal w/ them
I didn't think
I'm lost and
I'm sorry
They Know
Run

Last edited by jamer123; 2008-07-11 at 11:34 AM.
Old
Profile PM WWW Search
jamer123 shouldn't have fed itjamer123 shouldn't have fed itjamer123 shouldn't have fed itjamer123 shouldn't have fed itjamer123 shouldn't have fed it
 
 
jamer123
 
 

Bookmarks

« Previous Thread | Next Thread »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules [Forum Rules]
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Basic 6 Tutorials Lenny Science and Art 0 2008-07-11 08:27 AM
Tutorials !King_Amazon! RPGMaker 5 2008-03-04 08:29 PM
Tutorials List!~~ Bezier Science and Art 32 2007-11-28 02:54 PM
Basic of Basic's Executor[Gk] General Gaming 1 2003-03-22 08:41 AM
Need Photoshop Tutorials Mr.Lee Science and Art 1 2002-04-17 12:19 AM


All times are GMT -6. The time now is 02:41 AM.
'Synthesis 2' vBulletin 3.x styles and 'x79' derivative
by WetWired the Unbound and Chruser
Copyright ©2002-2008 zelaron.com
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.