Post by microfarad on Apr 13, 2010 9:08:53 GMT -5
Visual Basic, or VB, is a wonderful programming language. It isn't too difficult to learn, which makes it ideal for beginners, but it is also powerful, and suitable for professional software developers. This will be a multi-post tutorial in Visual Basic. All of my posts which include tutorial content (after this one) will bear the exclamation point icon.
Downloading Visual Basic:
Visual Basic 2008 Express Edition is free software made by Microsoft. Just Google search it, you'll find where to download it. They keep changing the location, so I can't give you a link.
Your First Program:
Open up VB and click File->New. A window pops up. In it are a bunch of templates you can use. You will almost always be using a "Windows Forms Application". Go ahead and select that one. In the textbox type your program's title "My First Program".
The Solution Explorer:
At this point you should see a blank window in the designer area. This is your form, where you can put buttons and stuff. You can have multiple forms in a project, but that comes later. Just click View->Solution Explorer. A toolbox appears with all the things in your project. Here you can add, delete, open, and modify all the little pieces in your project, such as your form. Go ahead and close the solution explorer, we don't need it yet.
Adding controls:
In addition to the Solution Explorer, there is another toolbox, called the "Toolbox". It should already be open, but if it isn't click View->Toolbox. In the Toolbox, select a Button. Click on your empty form to put the button on it. Do the same for a TextBox. Notice that you can click and drag your controls to change their position.
Properties:
Right click on your button and hit "Properties". A toolbox pops up with some options. Go down to where it says "Text", and put in "GO" instead of "Button1". Go ahead and close the properties window.
The gobbledygook:
Double click your button. Now your in the code window! Your code should look like this:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class
What is all this??? "Public Class Form1" declares a new class, this class includes all the code for your form, as well as the form itself. "Private Sub" declares a new subroutine. Since it is a "Private" sub, no other class can access it. The program would work just as well if you deleted the "Private", it's just to keep stupid humans in line. "Button1_Click" is just the name of the sub (subroutine). "ByVal" declares a parameter for the subroutine. Don't worry about parameters just yet, note however that there are two in this subroutine. "Handles Button1.Click" is a very special piece of code, but I'll get to it in a second. The "End Sub" and "End Class" pieces of code should be self explanatory.
Your first code:
It is time to write your first line of code! Put your cursor in the middle of the "Button1_Click" subroutine. Type an "A". WOAH! Did you see that box? That box my friend, is your instruction book. Scroll down to "Math", now click, not double click, on "Math". A little yellow tool tip appears describing the "Math"... thingy. We won't use Math yet, but this demonstrates how I learned Visual Basic. I just read the descriptions of a whole bunch of things. But back to our program, backspace your "A" and type "Button1." - do not forget the dot! Dots are all important in Visual Basic. All of the functions, subroutines, classes, namespaces, and everything else you will ever use is ordered into a hierarchy. A "." moves you one step down the hierarchy. After typing that "." we moved into the set of things that belongs to Button1. See there, in the box? (Oh magical box) There's the word "Text". This is the very property we changed in the designer window, but you can also read it and even change it in your program! But we don't want to change Button1.Text, backspace and type "TextBox1.Text" instead, yeah, we'll change that. Then type: <= "Hello World">, please include the quotes but not the <> brackets, I used those because I needed to add quotes into the line I was quoting, so I used brackets instead. The quote marks indicate that the value is a String, or a series of characters. Your program should now read:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "Hello World"
End Sub
End Class
A closer look at events:
For a loooooong time I used to stick code in like this, but never knew what made it run. What exactly tells the computer that the sub "Button1_Click" should be run when Button1 is clicked. The answer is this part: "Handles Button1.Click". "Handles" tells the computer to watch for the event following "Handles". The event is "Button1.Click". "Click" is one of those properties in the Button1 hierarchy. So now that we know what the heck it happening, let's run the program!
Running your program:
Just click Debug->Start Debugging. Your form appears right? Now click the button. TA-DA! Hello World. Go ahead and close your program. Click File->Save All to save. Go ahead and tinker around a bit, see what you can do. Just don't modify this program, we'll keep building on to it later.
Downloading Visual Basic:
Visual Basic 2008 Express Edition is free software made by Microsoft. Just Google search it, you'll find where to download it. They keep changing the location, so I can't give you a link.
Your First Program:
Open up VB and click File->New. A window pops up. In it are a bunch of templates you can use. You will almost always be using a "Windows Forms Application". Go ahead and select that one. In the textbox type your program's title "My First Program".
The Solution Explorer:
At this point you should see a blank window in the designer area. This is your form, where you can put buttons and stuff. You can have multiple forms in a project, but that comes later. Just click View->Solution Explorer. A toolbox appears with all the things in your project. Here you can add, delete, open, and modify all the little pieces in your project, such as your form. Go ahead and close the solution explorer, we don't need it yet.
Adding controls:
In addition to the Solution Explorer, there is another toolbox, called the "Toolbox". It should already be open, but if it isn't click View->Toolbox. In the Toolbox, select a Button. Click on your empty form to put the button on it. Do the same for a TextBox. Notice that you can click and drag your controls to change their position.
Properties:
Right click on your button and hit "Properties". A toolbox pops up with some options. Go down to where it says "Text", and put in "GO" instead of "Button1". Go ahead and close the properties window.
The gobbledygook:
Double click your button. Now your in the code window! Your code should look like this:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class
What is all this??? "Public Class Form1" declares a new class, this class includes all the code for your form, as well as the form itself. "Private Sub" declares a new subroutine. Since it is a "Private" sub, no other class can access it. The program would work just as well if you deleted the "Private", it's just to keep stupid humans in line. "Button1_Click" is just the name of the sub (subroutine). "ByVal" declares a parameter for the subroutine. Don't worry about parameters just yet, note however that there are two in this subroutine. "Handles Button1.Click" is a very special piece of code, but I'll get to it in a second. The "End Sub" and "End Class" pieces of code should be self explanatory.
Your first code:
It is time to write your first line of code! Put your cursor in the middle of the "Button1_Click" subroutine. Type an "A". WOAH! Did you see that box? That box my friend, is your instruction book. Scroll down to "Math", now click, not double click, on "Math". A little yellow tool tip appears describing the "Math"... thingy. We won't use Math yet, but this demonstrates how I learned Visual Basic. I just read the descriptions of a whole bunch of things. But back to our program, backspace your "A" and type "Button1." - do not forget the dot! Dots are all important in Visual Basic. All of the functions, subroutines, classes, namespaces, and everything else you will ever use is ordered into a hierarchy. A "." moves you one step down the hierarchy. After typing that "." we moved into the set of things that belongs to Button1. See there, in the box? (Oh magical box) There's the word "Text". This is the very property we changed in the designer window, but you can also read it and even change it in your program! But we don't want to change Button1.Text, backspace and type "TextBox1.Text" instead, yeah, we'll change that. Then type: <= "Hello World">, please include the quotes but not the <> brackets, I used those because I needed to add quotes into the line I was quoting, so I used brackets instead. The quote marks indicate that the value is a String, or a series of characters. Your program should now read:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "Hello World"
End Sub
End Class
A closer look at events:
For a loooooong time I used to stick code in like this, but never knew what made it run. What exactly tells the computer that the sub "Button1_Click" should be run when Button1 is clicked. The answer is this part: "Handles Button1.Click". "Handles" tells the computer to watch for the event following "Handles". The event is "Button1.Click". "Click" is one of those properties in the Button1 hierarchy. So now that we know what the heck it happening, let's run the program!
Running your program:
Just click Debug->Start Debugging. Your form appears right? Now click the button. TA-DA! Hello World. Go ahead and close your program. Click File->Save All to save. Go ahead and tinker around a bit, see what you can do. Just don't modify this program, we'll keep building on to it later.