Welcome to my VB Tutorial for Beginners. Let's get started
Things you will need:
- Visual Basic 2008/2010
- You'll have to atleast know how to work with VB
First Script
The very first script we'll be making is the typical "hello world".
1. Start VB
2. Create a new Project
3. Then in your windows form add a Button, and name it Start.
4. Then double click the button, and in the source code type in, the following:
Code:
MessageBox.Show("Hello World")
5. Run the program and click "start", that you already created. There should appear a new small window saying "Hello World".
In the Button source code there should be this:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Hello World")
End Sub
The private sub Button1, introduces the program to the Buttons function, which in this case is displaying a message box saying "Hello World".
This is basic coding, thats why it is the first script that we're writting.
Variables
A variable is a letter,number or sign that has a value. This value can be a number, word, string, etc.
As a variable it means its value can be changed into other value.
1. Start VB
2. Create a new Project
3. Then in your windows form add a Button, and name it Start.
4. Then double click the button, and in the source code type in, the following:
Code:
Dim intNumber As Integer
intNumber = 27
intNumber += 1
MessageBox.Show("intNumber + 1 =" & intNumber.ToString, "Variables")
5. Run the program and see what happens. There should appear, after you click the button, a little window displaying the message "intNumber + 1 = 28"
In the first line of code we declared an integer (Variable) to the program which is named as "intNumber" and has the value of 27.
After that we informed the program that, that same variable is equals to "it's value + 1".
We made a message box that will display the string "intNumber + 1 =" and after that we ordered the message box to display the "intNumber" as a string with ".ToString". The "Variables" is considered to be the title of the message box.
As i made with the plus sign you can make with the other signs, like this:
Times
Code:
Dim intNumber As Integer
intNumber = 28
intNumber *= 2
MessageBox.Show("intNumber * 2 =" & intNumber.ToString, "Variables")
Division
Code:
Dim intNumber As Integer
intNumber = 28
intNumber /= 2
MessageBox.Show("intNumber / 2 =" & intNumber.ToString, "Variables")
Minus
Code:
Dim intNumber As Integer
intNumber = 28
intNumber -= 2
MessageBox.Show("intNumber - 2 =" & intNumber.ToString, "Variables")
List Boxes
A list box, is a box which works as a List of Items.
1. Start VB
2. Create a new Project
3. Then in your windows form add a Button, and name it Add.
4. Create a TextBox
5.Create a List box.
Spoiler (Click to View)
6. Double click the button, and type this in the source code:
Code:
ListBox1.Items.Add(TextBox1.Text)
7. Run the program, type anything in the text box, and click the button "add". There should appear in the list box the string that you typed in the textbox.
As for the code it is quite simple, basically you're telling the program to add to the listbox everything that is written in textbox.
Radio Buttons
A radio button can become really usefull when you want the user to make a choice. And for this we're going to make an intro to the if statement.
1. Start VB
2. Create a new Project
3. Then in your windows form add a Button, and name it Check.
4. Add a groupBox And name it "Your Age"
5. Add 2 Radio Buttons and name the first one "-18" and the second one "+18"
Spoiler (Click to View)
Double click the Button, and type this in the source code:
Code:
If RadioButton1.Checked = True Then
MessageBox.Show("you are under 18|")
ElseIf RadioButton2.Checked = True Then
MessageBox.Show("you are over 18!")
End If
6. Run the program.
The if statement is really usefull because it introduces a possibility to the program making it easier to run choices such as radio buttons.
What we told the program to do:
1st. If the radiobutton1 is checked then messagebox will appear saing that you're under 18.
2nd. If the second radio button is checked, then a messagebox will appear saying that you're over 18.
Text to Speech
In this part of the tutorial i wont be teaching much of programming, instead i though of making something cool, like making a program that will speak whatever you type in a textbox.
Create a Project
1. Add a TextBox and a Button (name it "Spell it")
Here's an example:
Spoiler (Click to View)
2. Double click the button and type:
Code:
Dim SAPI
SAPI = CreateObject("SAPI.spvoice")
SAPI.Speak(TextBox1.Text)
3. Run it. Write something in the textBox and click the button. Listen to the computer speaking what you just wrote down.
This code is really simple. Basically you created a mini vbs file that is recognized as voice producer. And in the third line of code you're telling that voice producer to produce what is written in the textbox.
The For Loop
This part of the tutorial brings us to an intermidiate programming skills, it is easy to understand, but sometimes it gets a bit complicated to explain.
Start by the usual proceder.
1. Add a button and name it "loop"
2. Double click it and write this code in:
Code:
Dim i As Integer
For i = 0 To 10
MessageBox.Show("the value of i is:" & i)
Next
3. run the program.
Basically what this does is constantly show a message box displaying the integer "i" with its different values. But when its value reachs 10 the loop will stoped, as commanded in the "For i = 0 to 10".
Do while
The do while statement is simple as making something while something is "whatever". It really is just like that.
1.Add a button and name it "Do while"
2. Double click it and write this code in.
Code:
Dim num1 As Integer = 1
Do While num1 < 10
MessageBox.Show("the value of num1 is:" & num1)
num1 += 1
Loop
3. Run it.
What this does is Messagebox the value of "num1" while its value is under 10. So you will see many messageboxes appearing with different numbers from 0 to 10.
Do Until
The Do until statement is really similar to the do while statement. With just one difference, the do until statement runs the function until it reachs the limit you typed in.
1. Add a button, and name it "Do until"
2. Double click it and write this code in
Code:
Dim a As Integer
Do Until a > 10
MessageBox.Show("the value of a is:" & a)
a += 1
Loop
3.Run it
What you just made is, introduce the integer "a" and messagebox "a + 1" until "a" becomes greater than 10. After that the loop will end.
Subs
A Sub is a way for you to type a code and execute it later.
1. Create a Project
2. Add a Button and call it "Sub"
3. Double click the button.
4. Now under "Public Class Form1" type in the following:
Code:
Sub message()
MessageBox.Show("Used the Sub")
5. Now , in the button Private Sub just type in:
Code:
Call message()
You should get something like this code:
Spoiler (Click to View)
6. Debug the project, and click the button.
Now, there should appear the messageBox saying "Used the Sub", although it wasn't typed in the button Private sub, this happens because you declared the code before and called it after to be executed in the Button function.
Functions
A function is a piece of code that will allow you to get to a specific part of the program and change its contents.
1. Create a Project
2. Add a Button and name it "Function"
3. Add a Label.
4. Now in the Public Class Form1 type this code:
Code:
Dim labeltext As String = "Used the Function"
5. Bellow that you'll have to create a Private Function, where you will return the integer "labeltext".
Code:
Private Function ChangeLabel1Text()
Return labeltext
End Function
6. Code the Button.
Code:
Label1.Text = ChangeLabel1Text()
You should have a code similar to this:
Spoiler (Click to View)
7. Run it, and by clicking the Button you should see the Label changing to "Used the Function".
Explanation:
First of all you declared an integer named "labeltext" as string that will display "Used the Function". Than you created a Private Function that will Return the Integer "labeltext" to the program so it can be used by the button. And finnally the button will change the "Label1.Text" content, which is "Label1" to "Used the Function".
Advanced MessageBox
In this part of the tutorial, i'll be explaining how to make an Advanced MessageBox, by changing its title, icon and Buttons.
1. After creating a Project, add a Button and name it "Start"
2. Double click and in it type the following:
Code:
If MessageBox.Show("blablabla", "Acvanced", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
MessageBox.Show("youclicked yes")
Else : MessageBox.Show("you clicked no")
End If
3.I used the 'if' statement in this code, to display a new messagebox depending on each button the user clicks (Yes or No).
4. Run the program.
How the code works
-if the messagebox, which displays the text "blablabla", has the title "Advanced", has the buttons "yes" an "no", and has the icon of a question ('?'), has its button "yes" clicked, then the program will display another messagebox saying "You clicked yes", else the program will display another messagebox saying "you clicked no".
Here's is the function for each bit of code:
Code:
MessageBox.Show("blablabla", "Acvanced",
Creates a messagebox with the text "blablabla" and the title "Advance"
Code:
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Gives the messagebox, the 2 buttons "yes no" and the icon of a question ('?').
Conditional Operators
A Conditional Operator is nother the less then the:
'equal to' ('=')
'greater than' ('>')
'lesser than' ('<')
'not equal to' ('<>')
Curiosity: The "<>" Operator can be used in Log In programs.
1. Create a Button and name it "check"
2. Create a textBox.
3. Double click the Button and type in this code:
Code:
If TextBox1.Text <> "123456" Then
MessageBox.Show("you didn't enter the right password")
Else
MessageBox.Show("You entered the right Password")
End If
What this code does is, check if the password that you entered in the textbox is the correct one or not. In this case if you type something other than "123456" the program will display the messagbox saying that you typed the wrong password, and if you type the correct password, it will display a messagebox saying so.
Timer
A timer is an option that comes with Visual Basic that allows you to, well.. make a timer. And you can do it this way:
1. Create a project and add 2 Buttons and 1 Textbox.
2. Name Button1 "Start" and Button2 "Stop"
3. Change the textbox text to "1"
4. Add a Timer.
5. Now double click the "start" Button and type in this code:
Code:
Timer1.Start()
6. Go back to your form and double click the "stop" button, and type in this code:
Code:
Timer1.Stop()
7.Go back to your Form, and this time double click the timer icon which is located in the bottom of the screen. And type in this code:
Code:
TextBox1.Text += 1
8. Go back to the form, and in the timer properties change the interval To whatever you want ( 1000 = 1 second ) this will determinate the time interval which with it the timer will execute the code over and over again. Try and set it to 1000.
9. Finally run the program and click "start".
I am not going to explain this code, because it is easy to understand, and if you followed the tutorial properly you should be able to understand this one by yourself.
Make a Web Browser
What i'm going to teach you now is how to Create a Web Browser.
Let's get Started.
1. Create a Project, Make your Form a BIG one.
2. Add 5 Buttons, a TextBox and of course a Web Browser.
3. Make it like this:
Button1 -> "Visit URL"
Button2 -> "Back"
Button3 -> "Forward"
Button4 -> "Refresh"
Button5 -> "Homepage"
Example:
Spoiler (Click to View)
Now do exactly as this:
Button1 "Visit URL"
Code:
WebBrowser1.Navigate(TextBox1.Text)
Button2 "Back"
Code:
WebBrowser1.GoBack()
Button3 "Forward"
Code:
WebBrowser1.GoForward()
Button4 "Refresh"
Code:
WebBrowser1.Refresh()
Button5 "Homepage"
Code:
WebBrowser1.Navigate("YourHomepagehere")
4. Now in the webrowser source code type the following:
Code:
TextBox1.Text = WebBrowser1.Url.ToString()
This code will make the textbox change its content acording to the web page your visiting.
Run the program and test it