Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 Get you started on VB
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude

PostPosted: Wed Jul 05, 2006 9:18 pm   Post subject: Get you started on VB


Hi and welcome to the world of Visual Basic (VB). Remember not Visual Basics. This tutorial will kick start your learning in Visual Basic in no time. This tutorial is meant to teach you the basics that will let you experiment and understand Visual Basic more. So lets get started!

Objects and Events
You might be wondering what in the world are objects? you hear this word so many times but you might have never known the meaning. the easiest way to explain what an object is would be to tell you to look at the left side of your visual basic screen on the vertical toolbar. you will see something called "command button" and something called a "textbox" and other things that you probably have no clue yet what they do and what they're for. all those things are objects. they perform tasks on cetain data.

An Event on the other hand is any action a user can perform, which objects respond to. once you open your code screen by double clicking on an object i.e. button, you will see in the drop down arrow different event to choose from. example are

1) click - this is when the user can click on an object and something will happen
2) double click - same as click only you must double click on object for something to happen

· When you create an object you must give it a meaningful name
o Buttons
o Text boxes - txt_textboxname
o Forms - frm_formname
o For example: an exit button would be named cmd_exit
o To name an object use the name property

if you look at the right side of the screen close to the bottom, you will see a list of different properties. the first one on the list is Name which is used to give the name of how you will refer to the object in your code.

some common properties you will be using when your starting out are:
· Back color - controls the background color of the whole screen
· Caption - the text that appears in the form's title bar
· Window state - controls how big the window is when the program is run 0 normal, 1 minimized, 2 maximized (best)

These properties are very important to making the screen look right and to making the program act right.

Sorry i'm rushing now, so i will continue this tutorial tommorow. I Hope this will help you get the basics right away.
cool dude

PostPosted: Thu Jul 06, 2006 11:58 am   Post subject: (No subject)


When you open Visual Basic for the first time you will see the form first, where you put object on. to begin writing code you can double click on the object and visual basic automatically sets up the subprogram header. The header looks as follows:


Private Sub ObjectName_Click ()

        'The code that will run when the event happens

End Sub

lets explain the header above.
Private - subprograms is used only in this form
Sub - subprogram
ObjectName - what you name the object
Click - the event or action that the user takes that will make the code run

Your commands go in between Private Sub and End Sub. Commands are the lines of code you type in order to get the program to perform as you desire.

· Never erase or change Private Sub and End Sub
· Always rename your objects before coding them or the code will not be executed for the correct objects

Some sample lines of code include:

· End - terminate the execution of the program (better ways later)
· - show frmcoolDude on the screen (move to new question in Quiz program)
· txtName.Font.Bold = True - change the text in txtName to bold
· txtName.Font.Size = 24 - change the size of the text to 24
· txtName.Text = "Hello, World!" - change the text itself to hello, world!


comments are just there to help you understand what parts of code are doing and explain purposes of variables and code. you may ask why would i use comments, its just more work for me and it's really annoying. There are several reasons to use comments:
1)Help you understand your program better
2)in a year if you look at your program you might not understand what anything means
3)Helps people help you better! Smile

to make a comment in Visual Basic an apostrophe at the beginning of a line indicates the comment. Remember comments are for you! VB ignores the comments.

There are many expectations for comments, depending on your teacher. here's one expectation.

At the beginning of every program:

'Program title:  VB Tutorial
'Written by:        You're Name
'Date Written:    The current data of completion
'Purpose:       A descriptive of what the program does

Important - Long comment lines are to be broken at a logical spot and a new line started, do not let you lines get so long that they word wrap when printed or so that you need to scroll right to read them.

At the beginning of every sub program:

'Sub Program:      VB Tutorial - Day 2
'Written by:        Your name
'Date Written      The current date of completion
'Purpose:       A descriptive of what the Subprogram does
'Input      None
'Output  None
'Subprogram input:      None
'Subprogram output:     None

Important rules for comments
· Use full line comments to document your program.
· Do not write a comment above every line of code
· Write comments above blocks of related code such as dim statements, if blocks, for blocks, do blocks, blocks for calculations, blocks of output statements
· Your comments must explain the algorithm of your program
· A person should be able to read your comments, without looking at the code, and understand how your program works.
· Comments are written in English and must not simply be a copy of the VB code
· Comments must also explain why you are doing the things you are doing. They should not simply be a long translation of code
· Declare variables on separate lines so that you can explain each one

Look at this example:
Dim x as integer 'the x coordinate of the square

if i did not put a comment and then asked for help people would be asking me what does x mean? Therefore i need to put a comment. if your variable name is self explanatory you don't need to put a comment.

Math Commands
· + (add)
· - (subtract)
· * (multiply)
· / (regular division with deciamals)
· \ (division with no deciamals
· mod (remainder from division)
· ^ (exponents)
· sqr (square root)

Day 3 will be posted later today or tommorow. I Hope your enjoying this tutorial so far Smile
cool dude

PostPosted: Fri Jul 07, 2006 12:46 pm   Post subject: (No subject)


· Variables are used to store data/information
· Variables can be:
o integer - whole numbers, no decimals, negatives and positives
o Long - large integers
o Single - real numbers (all numbers including decimal numbers)
o Double - large real numbers
o Currency - for money
o String - character data (any character on your keyboard - can be letters and numbers) and cannot be used in calculations
o Boolean - true or false
o Variant - for any variable type - can change

examples of declarations
Dim num as integer
Dim name as string
Dim average as single

***VARIABLE NAMES MUST BE DESCRIPTIVE - i.e. the variable name describes the data stored in the variable. Also use comments to explain what the variables represent

constants are like variables except there value never changes. ex. pi = 3.14
to declare a constant we use the command "const"

for exampe:
Const pi = 3.14159265

In Visual Basic there are many ways to display outputs. some ways are:
message box
list box

Let's look at how to display output using a message box. really simple!
call MsgBox("Message to appear", vbOKOnly)

now lets look at how to display output using a textbox. also really simple!
txt_TextBoxName.Text = value to appear in text box

Lastly lets look at how to display output in a label box.
lbl_LabelName.Caption = Value to appear in label

Formatting Output
As with any programming language we want to be able to show our output in an appropriate way. For example: limiting the number of decimal places, displaying as currency, dates, etc.

following is an example of displaying the output as currency.
ObjectName.Property = Format (value, "currency")

this displays the output to have 2 decimal places
ObjectName.Property = Format (value, "0.00")

allows user to provide information to the program
writing in text boxes is the most common way to allow users to input information
use the Val command to accept input that is numbers

it takes people some time to grasp the way the input works. basically if i entered my input in a textbox names txt_coolDude then the input will be stored under that name. therefore if i wanted to display the input i entered in txt_coolDude in a message box i would say:

Call MsgBox(txt_coolDude.Text, vbOKOnly)

More of Formatting output
you may or may not care about how nice the output looks, but if you want your program to be user friendly then here are some useful formatting techniques to make your output look much better!

· If you do not want the user to change the contents of the output, then the ENABLED property of the textbox can be set to false
· If your output will be more than one line long, then set the MULTILINE property of the textbox object to true
· If your output will be many lines long and your textbox is too short to display all the lines of output, you can add a scrollbar to the textbox object, set the SCROLLBAR property to vertical
· To join together many strings into one output line, use the string concatenation operator (+)
· To assign non-strings to the textbox text property, use the convery-to0string built-in function (CStr)
· To start a new line, use the vbNewLine constant
· To format a string with an exact number of output places, use the format command with @to indicate the room the output should take, such as Format ("Cool Dude",!@@@@@@@@@@@@")
· To format a number with an exact number of output places, us the format command on the converted-to-string value, such as Format (CStr(50),"!@@@@@@@@@")
· The ! in the format command indicates the output should be left aligned

Here are some examples you can look at to understand more about formatting output:

txtOutput.text = "Hello there"
txtOutput.text = "Hello" + " there"
txtOutput.text = "Hello"+  name
txtOutput.text = "Hello " + name +vbNewLine + "goodbye now"
txtOutput.text = "Hello " + name + vbNewLine
txtOutput.text = txtOutput.text + "goodbye now"
txtOutput.text = "my age is " +CStr (16)
txtOutput.text = Format (name, "!@@@@@@@")

Well thats it for Day 3. come back to see Day 4 either later today or tommorow. Smile

PostPosted: Fri Jul 07, 2006 2:03 pm   Post subject: (No subject)

You should be careful to note which version of VB you're discussing. VB6 or VB.Net.

For instance, if you are using VB.Net, then it's possible to output to standard output (the Console class), and not just the four examples you provide.
cool dude

PostPosted: Sat Jul 08, 2006 10:29 am   Post subject: (No subject)


Before i continue with my tutorial, i would like to clarify that this tutorial is for Visual Basic 6.0. Thank you wtd for reminding me. However, if you are learning you can still go through this tutorial because a lot of the basics are very similar.

There are several ways you can randomize a number, in fact some people may argue that my way is not the best, but i think my way is the easiest, because there is a built in function that randomizes a number for you!
Randomize 'only once executed in program
Num = int (10*Rnd) + 1

Let's explain the above line.
lnt - takes the integer value
Rnd - generates a random real number between 0 and 1 (0 is valid number but 1 is not)
10 - Upper bound
1 - Lower bound

Mid function - Breaking up strings
Lets say you wanted to reverse a word or get the middle of the word or anything else that would involve getting certain letters out of a word, you need the mid function.

Lets see an example!

myStr = "Pineapple"
mid (myStr,3,4) = NEAP

As you can see in the above example the first number represents the starting position which was 3 and the second number represents how many letters to take which was 4 characters.

Here's another example:

myStr = "Pineapple"
mid (myStr,5,5) = APPLE

Starting up
this should have been in my day 1 tutorial, but i forgot to add this. these are the steps to start up Visual Basic.
1. open visual basic V6.0
2. start a new project by choosing standard EXE
3. you now have a blank form
4. Design your form by choosing different controls/objects:
5. define the Properties for each object:
6. write the code
a. double click the object and visual basic automatically sets up subprograms
b. fill in

There are 3 main types of loops. loops are basically used to repeat portions of code a number of times. the three types are counted loop (for loop), conditional loop, and unconditional loop.

Explanation of each loop
1. counted loop
· run a specific number of times
· the number of times to execute is known before the loop starts
· for - next
2. conditional loop
· continues to execute until a specific condition is met
· do while (or until) - loop, do - loop while (or until)
3. unconditional loop
· continues to execute forever
· never stops on its own
· endless loop
· should never be required

counted loop example

for i = 1 to 10 step 2
    lst_Num.additem i
next i

The above output should look like this:


you may notice that it doesn't go up to 10. the reason is because i made it increase by 2 every time and if it went one more time it will actually be 11, thus we don't execute it again and stop at 9. if you wanted the loop to increase by increments of 4 you would say step 4. easy as that! don't forget that if your going backwards and want to go from 10 to 1 you will have to say step -1

Conditional loop example

    Dim num As Integer
    num = 10
    Do Until (num = 0)
        lst_num.AddItem num
        num = num - 1

in the above example the loop keeps repeating to subtract one from 10 until the number = 0.
this is the output:


you may notice it doesn't display 0 and it is because when it subtract 1 from 1 it = 0 and then it checks if num = 0 and that is true so it stops and doesn't actaully display the 0.

Now look at this example. it seems exactly the same except for one change. can you spot it?

    Dim num As Integer
    num = 10
    Do Until (num = 0)
        num = num - 1
        lst_num.AddItem num

now it will display the same output with the 0. this is because it subtract 1 from 1 and then displays the number 0 and then checks if the number = 0.
this is usually the most confusing part for people so try and wrap your mind with these 2 similar yet different examples.

Well Day 4 tutorial came to an end. Day 5 should be coming tommorow possibly. if you have any questions, comments, or just anything useful to add to this tutorial please do. Enjoy!

PostPosted: Wed Oct 25, 2006 1:11 pm   Post subject: (No subject)

Great tutorial! Teaches me more than my teacher!

Oh, as for the math commands, your missing a few...

> - Greater Than
< - Less That
<> - Not Equal To (simply known as Not)

Keep adding to this!
Silent Avenger

PostPosted: Wed Oct 25, 2006 1:53 pm   Post subject: (No subject)

The tutorial teaches more than your teacher? Wow either your teacher has never programed before, has never teached before or both if your teacher isn't able to teach as much as this tutorial does. But anyways this is a very nice tutorial so I hope you keep it up cool dude.
cool dude

PostPosted: Wed Oct 25, 2006 4:14 pm   Post subject: (No subject)

Silent Avenger wrote:
The tutorial teaches more than your teacher? Wow either your teacher has never programed before, has never teached before or both if your teacher isn't able to teach as much as this tutorial does. But anyways this is a very nice tutorial so I hope you keep it up cool dude.

thanks. if i know ppl are interested then i'll add some more on chrismtas break because i have no time now with friking chemistry work. anyhow any specific topics you guys want to learn?
Silent Avenger

PostPosted: Wed Oct 25, 2006 5:56 pm   Post subject: (No subject)

Well I'm pretty good at VB so some of this I already know but it's a really good tutorial for review for a test and especially the exam because some of the simple things can get you. But I think that having the with property, the common dialog box, the use of a User Defined Type, and you could also introduce the applications property which is very usefull for loading files eg.(App.path & "filename"). I'm just suggesting these because they'll be useful for some people for their ISUs Wink

PostPosted: Thu Oct 26, 2006 12:00 pm   Post subject: (No subject)

Silent Avenger wrote:
The tutorial teaches more than your teacher? Wow either your teacher has never programed before,...

Lol, she is just as new to VB as the rest of us. As I type this now, we're learning about loops (allready know these, thanks to cool dude) and shes reading it all from the book, word for word. Just give me the book and let me read it, i'll learn faster that way.

Or even better, keep going cool dude!

As for suggestions on topics to learn, how bout graphics? May be a little advanced, i think.
Silent Avenger

PostPosted: Thu Oct 26, 2006 3:33 pm   Post subject: (No subject)

Yes graphics would be a good thing to cover especially for some of those that are making games where they are using filled shapes, lines, boxes, etc. (eg. Mini-Putt game) You could also include saving files later on in the tutorial, for those people who would like to save a high score for their game.

PostPosted: Fri Aug 01, 2008 1:48 pm   Post subject: RE:Get you started on VB

Thanks for sharing all this useful information.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to: