Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > Articles, Tutorials & Reviews > New Article Submissions
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


New Article Submissions Where new Articles are submitted and are pending for review

Closed Thread
 
LinkBack Thread Tools
Old 12-23-2008, 04:19 PM   #1 (permalink)
Mentor, Articles Team
 
TheAtheist's Avatar
 
Join Date: May 2006
Location: Portishead, Bristol
Posts: 1,312
OS: xp sp3

My System

Visual Basic Tutorial

Visual Basic Introductory Tutorial

In this article we shall cover some of the basics of Visual Basic programming. Despite what the name of the programming suggests, Visual Basic can be used for very complex programs. ]
BASIC stands for Beginner's All-purpose Symbolic Instruction Code, and the visual element is due to the fact that the programming relies on a graphic layout, as the Tutorial shall demonstrate later.

The first bit of coding we shall look at is setting up a messagebox to display a message.
Hello world msgbox
Code:
Msgbox       Prompt, [buttons as VbMsgBoxStyle =       vbOKOnly], [      Title], [helpfile], [context], As VbMsgBoxResult
  • Prompt is the actual text you want the msgbox to display
  • This first major variable tells the compiler (visual Basic) which type of buttons or sometimes the style you want on the MsgBox. As default this is set to vbOKOnly. After entering the prompt and a Comma and a space, you get a dropdown box of all the possible options here. It’s easier for you to do this by trial and error, as most of these are fairly self explanatory, than to simply exhaustiavely list all of them.
  • The “title” variable dictates, as the name suggests, allows you to change the title text displayed in the blue bar at the top of the message box.
  • This part of the Msgbox is far beyond the scope of this tutorial, so we shall simply ignore it in this case, as we will with context.
  • The final part of the MsgBox statement simply sums up what this function does, we do not need to worry about its effect or do anything to code with it.
So, now you understand all the components of this statement, lets code a Msgbox command. Firstly, on the General Toolbar on the left-hand side of the Visual Basic screen, Click on the Command icon, the Rectangle grey button, as shown below.


Move the cursor back to the main Form view area. A Crosshair cursor should now replace the normal arrow cursor. Click and drag to place the command button.


This should look something like the image on the left, although the placing and size is entirely up to you.


To change the text displayed with Command Button, we need to go into the properties area. This is by default on the right hand side of the screen, and usually in the middle of all the other sections, although it can be detached. If you can’t see this, you need to do the following to make it appear.
View | Properties Window
Or press the F4 key on the keyboard.
To change the text on the button, we are concerned with the property called “caption” On the right hand side of the properties bar is the text currently in the box. We simply select this, and start typing what we want in there. For this exercise, this can be anything you want.

To code for the Msgbox, go back into the Form Mode by pressing SHIFT + F7
Double click on the Command button again, and you will be presented with your first page of code. By double clicking on that button, the compiler adds the start and end bits of code that allow that button to do anything.


Code:
Private Sub Command1_Click()
Msgbox “Hello World”, vbOkOnly, “Hello World”
End Sub
This should display the following Message box. If it does not, you will need to do some basic troubleshooting. Check that all the spaces are in the correct places, there should be a space after ever comma in the code, not before, although if you do put a space before a comma, the compiler will automatically remove it. Diagnosing issues in the code at this early stage is crucial, because as code gets more and more complicated tiny errors can cause massive headaches. We’ll look at error trapping solutions later on.


You have written your first bit of code. In the next section you will look at conditional coding, which is crucial part of many programs.

Conditional Statements:
Unless you are programming a completely static program, where only one set of outcomes can arise, you will need to use conditional statements of some kind. The most basic type is the IF statement, and this is how it works and how we shall use it.
The logic of an If statement is as follows:
Code:
If Statement is (whatever we choose to test, usually true or false) Then do the following
Code
Else ( the above statement resolves as false)
Do this code
End the if statement
This is pretty much exactly the code we use. To make this really effective, we will be combining If statements with the msgbox we did above (with a bit added)
Project: The idea of this project is to display a Msgbox, to capture the result of this MsgBox and display a relevant response in the program.
The first job is to create variables, pieces of data stored by the program that allow us to do something dynamically and store responses to certain events in the program.
View | Code
Just above the area containing the code there are two dropdown menus. On the left hand one, select “General” The dropdown menu on the right will automatically change to “declarations” There is now a space above the Msgbox code we did last. Enter the following code:
Code:
Dim response As Integer
The Dim bit tells the compiler we want to define a variable here. response is the name of the variable and “As integer” tells the compiler that we shall be inputting a number into this variable at some point. Notice that the variable doesn’t contain any data yet, it simply exists. You will need this later.
Still in Code view, go back to this sub we wrote earlier
Code:
Private Sub Command1_Click()
Msgbox “Hello World”, vbOkOnly, “Hello World”
End Sub
The changes needed to get this to store a result and then turn it into something the user can see, are small, comparatively.
Code:
response = MsgBox("Hello World", vbYesNo, "Hello World")
instead of :
Code:
Msgbox “Hello World”, vbOkOnly, “Hello World”
To display some useful outcome to the user we need to interpret the value of "Response". Yes or No are represented in a msgbox by either a 6 or 7 respectively
Returning to
Code:
If (response = "6") Then
MsgBox ("Yes")
Else
MsgBox ("No")
End If
If we were to take this to its extreme, we would really need an ElseIf statement instead of the Else statement to check the other value was indeed a 7, however in this case the value cannot be anything else, so this is good enough for our purposes.

[end of article]
__________________


"Freedom of thought is best promoted by the gradual illumination of men's minds, which follows from the advance of science" - Darwin

Join the TSF folding team - Team 85015

Last edited by TheAtheist; 01-02-2009 at 04:13 PM.
TheAtheist is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 12-26-2008, 12:50 AM   #2 (permalink)
Manager Home Support, Assistant Manager Articles Team
 
DonaldG's Avatar
 
Join Date: Aug 2007
Location: Somerset, UK
Posts: 10,164
OS: XPsp2 Laptop & Vista Ultimate Desktop

My System

Blog Entries: 1
Re: Visual Basic Tutorial - WIP - images to be added soon

Nice one Joe. Which version of VB is it?

Can I help out with imaging?
__________________
.

Lest we forget...

"They shall grow not old, as we that are left grow old;
Age shall not weary them, nor the years condemn.
At the going down of the sun and in the morning
We will remember them."



DonaldG is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Old 12-26-2008, 09:28 AM   #3 (permalink)
Mentor, Articles Team
 
TheAtheist's Avatar
 
Join Date: May 2006
Location: Portishead, Bristol
Posts: 1,312
OS: xp sp3

My System

Re: Visual Basic Tutorial - WIP - images to be added soon

Quote:
Originally Posted by DonaldG View Post
Nice one Joe. Which version of VB is it?

Can I help out with imaging?
Doh, how did i forget that detail! - Its for VB6. All the images are being sorted right now, using my newly aquired Photoshop Pro!
__________________


"Freedom of thought is best promoted by the gradual illumination of men's minds, which follows from the advance of science" - Darwin

Join the TSF folding team - Team 85015
TheAtheist is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Old 03-05-2009, 02:59 PM   #4 (permalink)
Mentor, Articles Team
 
TheAtheist's Avatar
 
Join Date: May 2006
Location: Portishead, Bristol
Posts: 1,312
OS: xp sp3

My System

Re: Visual Basic Tutorial - WIP - images to be added soon

Any more comments on this one from any member? This ones been sitting around a while and i'd like if possible to get it published soonish if no one objects to that.
__________________


"Freedom of thought is best promoted by the gradual illumination of men's minds, which follows from the advance of science" - Darwin

Join the TSF folding team - Team 85015
TheAtheist is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Old 10-21-2009, 04:09 PM   #5 (permalink)
Mentor, Articles Team
 
TheAtheist's Avatar
 
Join Date: May 2006
Location: Portishead, Bristol
Posts: 1,312
OS: xp sp3

My System

Re: Visual Basic Tutorial - WIP - images to be added soon

Despite what i posted above, i'm going to rewrite this whole thing as there is a much more upto date version called Microsoft Visual basic 2008 - it's free as well!!!
__________________


"Freedom of thought is best promoted by the gradual illumination of men's minds, which follows from the advance of science" - Darwin

Join the TSF folding team - Team 85015
TheAtheist is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Old 10-24-2009, 02:50 AM   #6 (permalink)
Manager, TSF Articles
 
JohnthePilot's Avatar
 
Join Date: Mar 2006
Location: Cheltenham, near Wales.
Posts: 30,867
OS: XP Home SP3, PCLinux, Vista Home Premium SP1

My System

Blog Entries: 1
Send a message via MSN to JohnthePilot
Re: Visual Basic Tutorial - WIP - images to be added soon

Shall I hang fire then?
__________________


If you feel that TSF has helped you please make a donationand help to keep the forum free
Cenedl heb iaith, cenedl heb galon

JohnthePilot is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Old 10-24-2009, 08:32 AM   #7 (permalink)
Manager Home Support, Assistant Manager Articles Team
 
DonaldG's Avatar
 
Join Date: Aug 2007
Location: Somerset, UK
Posts: 10,164
OS: XPsp2 Laptop & Vista Ultimate Desktop

My System

Blog Entries: 1
Re: Visual Basic Tutorial - WIP - images to be added soon

I'd be keen to see one on VB 2008....Better get the pencil sharpened!
__________________
.

Lest we forget...

"They shall grow not old, as we that are left grow old;
Age shall not weary them, nor the years condemn.
At the going down of the sun and in the morning
We will remember them."



DonaldG is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Old 11-02-2009, 11:28 AM   #8 (permalink)
Mentor, Articles Team
 
TheAtheist's Avatar
 
Join Date: May 2006
Location: Portishead, Bristol
Posts: 1,312
OS: xp sp3

My System

Re: Visual Basic Tutorial - WIP - images to be added soon

Quote:
Originally Posted by JohnthePilot View Post
Shall I hang fire then?
If you wouldn't mind John

I'm going to run it this evening, see whats different in how i do what i did in vb6 and edit it that way!
__________________


"Freedom of thought is best promoted by the gradual illumination of men's minds, which follows from the advance of science" - Darwin

Join the TSF folding team - Team 85015
TheAtheist is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Closed Thread


Thread Tools

Posting 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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 08:17 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85