This tutorial will teach you how to make a nice little program in VB. I wrote this tutorial and came up with it by myself along with the idea and the code. This is in no way plagiarism so I don't want to be blamed for it. I put alot of work into my blog posts.
Now guys. My weapon of choice when it comes to Software Programming is definitely Visual Basic .Net
I know C# , C++, VB, BASIC, ACTIONSCRIPT 2.0 and many other types of languages. But Visual Basic will always be my favorite because I can never find any negative points about it. The possibilities are infinite mainly because the code is pretty easy to debug and the software helps you along the way.
Now for this tutorial you will need Visual Basic Express 2005.(Don't worry its free)
Visual Basic Express 2005 is free and is distributed my Microsoft. You do have to register(But registration is free anyway).
Step 1- Go to http://www.microsoft.com/express/2005/ and download the Visual Basic 2005 Express edition.
Step 2- Install it. You only have to register after 30 days of use so no worries about doing it now. (Unless you want to)
If everything went well. Visual Basic Express 2005 should be installed.
Now lets make a program!
Step 1- Open up Visual Basic Express.
Step 2 - Beside "Create: " click on project.
Step 3 - A window should Pop Up showing you a few choices. For this tutorial we will choose "Windows Application"
Step 4- Take a minute to get familiar with your work environment. The panel to the left is the "Toolbox" . In there you will find all the usable windows components like Buttons, Text Boxes, Picture Boxes, Radio Buttons, Check Boxes, Etc. This is where all your components will come from. To create a new component in your window("Form1") just grab an item with your cursor and drag it into your program.
Now on the bottom of the screen is the Error list. This is where Visual Studio will inform you of any Syntax, casting, or any type of errors in your code.
The Top Right is the solution Explorer. This is basically showing you all the components of your project, such as Form1 which is one of your windows. You can have multiple windows in a program but we will cover that in another tutorial.
The Bottom Right is very important. It is the Properties window. Here you can modify settings of each of your components (Such as your Form or one of your buttons) to view the properties of certain items you must select that item and then the properties window will automatically update. If you want to change the title or text of a button or your form etc. you must change the value "Text" not the value "Name". The value "Name" is just the unique identifier for each object so you can call them from your code. For example [code]Me.Button1.Text = "Click Me!"[/code] That code would change the text of Button1(If it existed in your program) to Click Me!
The Play, Stop and Pause buttons are for running your program. Play will execute the program. Pause will Pause your program without closing it. And Stop will completely stop your program(Useful if your program freezes).
That basically covers the GUI.
Now lets make a simple program(But not too simple). This isnt going to be some stupid "Hello World" Program. No a fifth grader can do that. Lets spice it up a bit.
Step 5- Now drag a button into your form and change its NAME to "btnGo"(I just like naming all my buttons with a Btn prefix.). Now change its TEXT to "Go!". Now that you have done that. Put the button in a place where you are happy with it.
Step 6- Now Drag a Label into your form. Give it the NAME "lblMsg"(I name all my labels with the lbl Prefix). Now give it the text "Hello (Put your Name Here)"
Step 7- Ok now this is kind of fast learning but its good for your brain. We are going to introduce a new component to you. The Timer element! I love this component. If the Timer component Didn't exist it would be much harder to make video games. Ok so get ready! Go to the toolbox and under the components collection Drag the timer out and drop it anywhere on your program.
Step 8- Instead of the timer showing up on your form, it is a component that runs in the background. it shows up on a little bar in your work area. Click on it and give in the NAME "TimePhysics"(I name all my timers with the Time prefix). Set its interval to 1(The interval is measured in milliseconds).
Step 9- Ok, instead of a normal tutorial that teaches from easy to hard. Lets do it the other way around. Lets cover the hard part first.
Double click on the timer. You will notice the whole screen changes. You can always get back to the visual designer by clicking on its tab.
So here we see some code going on.
Private Sub TimePhys_Tick(byval blah blah blah...)
End sub
Between those lines is what is executed every time the timer ticks.
So lets define some variables first. Right below public Class form1 is where you define global variables meaning the variable can be read and modified by anything in that Class(Form1).
This is how you define a variable.
Dim (Variable Name) as (Type of variable)
The most common variable types are:
String: Line of letters. (example: "This is a String")
Integer: Any number without a decimal positive or negative( Example: 51)
Boolean: A true False statement. ( YouAreSmart = False )
Float: any number that is a decimal( Example: .34)
There are a lot of variables but we will only need Booleans for this tutorial.
So go ahead and define these two variables under Public Class Form 1
Dim goingleft as boolean = true
Dim goingright as boolean = true
Now we have those important booleans defined! Now we can use them in our timer.
Step 10- Go to Private Sub TimePhys Tick(Byval Blah blah etc etc.
Under it type in this.
If Me.lblMessage.Left < 0 Then
goingleft = False
End If
If Me.lblMessage.Left > (Me.Width - Me.lblMessage.Width) Then
goingleft = True
End If
Ok let me explain what this does. Me.LblMessage.left is the x position of the label relative to the Form. This is how we control its position. Same with me.lblmessage.top (The Y position).
That block of code basically detects if the label is hitting the left or right edge of the form
If it does collide with the edge it switches the value of the boolean(The booleans control the direction of the label.).
Now this Copy paste this code under our already typed code. This does the same thing but Detects the top and bottom edge.
If Me.lblMessage.Top < 0 Then
goingdown = True
End If
If Me.lblMessage.Top > (Me.Height - 40) Then
goingdown = False
End If
Take note that the (Me.height - 40) part is like that so the label wont leave the form(It might but just adjust the number until it works to your happiness.)
Ok. Now that we have the collision detection in lets get this label some motion.
What this next block of code will do is check the boolean values to find out which direction the label should move in and moves it accordingly.
If goingdown = True Then
Me.lblMessage.Top = Me.lblMessage.Top + 2
Else
Me.lblMessage.Top = Me.lblMessage.Top - 2
End If
If goingleft = True Then
Me.lblMessage.Left = Me.lblMessage.Left - 2
Else
Me.lblMessage.Left = Me.lblMessage.Left + 2
End If
See basically if we want to move the label upward. it would be Me.lblMessage.Top = me.lblMessage.Top - 2(Or whatever you want to step it by measured in pixels.) Because we are taking its old value and adding or subtracting to it. Resulting in a change in its position.
YES! Now we have the (Crappy) physics engine implemented. Now we can add functionality to the button.
Step 11- THE HARD PART IS OVER GUYS! Now all we have to do is add functionality to the button which is easy. Go back to the Visual editor. And double click on the button. It should now go back to the code windows and your I-beam should be positioned between a new Private Sub. In here lets put a small block of code.
If Me.TimePhys.Enabled = True Then
Me.TimePhys.Enabled = False
Else
Me.TimePhys.Enabled = True
End If
That is a simple piece of code. All it does is reverse the Enabled boolean which is a private variable contained in the Timer element. So if its enabled and you click it, its disables it. And vice versa.
Step 12- This is the last step. Go to the Visual Editor and click on the timer(Single Click). Make sure the Enabled property is False. If its True, set it to false.(So the label will be still when we start the program.
Ok, get ready to see your first(Or maybe not your first) program come to life. Click the Green play button and VS Express 2005 will compile your program. When it shows up. Click your button. And if the Text starts to bounce around, CONGRATULATIONS! You just finished making a nice little program.
Oh and If you had trouble I have made a zip file of the VB project for this tutorial. www.dev32.net/Tutorial1.zip
Download it and see what you did wrong.
So What do you think? This tutorials was pretty informative right? Well I hope you enjoyed it. I will be writing 3 tutorials a week and posting them to my website www.dev32.net
Everyone is welcome.
Enjoy it fellas. And if you aren't a member. Sign up. We need you.
Tags: basic, fun, games, hello, help, program, software, tutorial, tutorials, vb.net
Share
You need to be a member of Reviews, News, and How To Geeks to add comments!
Join this Ning Network