Saturday, March 26, 2011

1.0 Getting Started - Visual Basic - Creating your first form

Before we get started I would like to give some information on my background. I am currently a computer science student; just finishing up my second year. I code primarily in Java, PHP, HTML, Javascript, and SQL. These Tutorials will be using Visual Basic and the .NET framework because .NET is a native windows language and has no restrictions. Java runs in a "sandbox" so to speak, so registry files and big changes to the computer are very limited. That is why I will be doing mostly tutorials for VB .NET and if this catches on, I could whip up some Java/PHP/etc. tutorials, or even do some requests later on.

Alright, so here we go--

STEP 1: Getting VB Express 2010
Click on the first arrow, select the language and install.

STEP 2: Creating a form with simple functionality
Go ahead and open up VB Express. Select new project, and name is hello world


You should see a window appear inside VB called "Form1" This is the GUI of your program. Look to the left, here you will see your "Tool Box" It is full of every little tool you could wish for. For the sake of user friendliness, click to open the tool box, then right click and select "dock" as shown:



This will be used a lot and it is good to see all the functions we can add. You probably can guess what a lot of these already do.


Now to the bottom right you will see the "Properties Page" This IMHO is what makes VB so great, you don't have to define every parameter with code, You can simply change the settings using a drop down list! Above this you will see your "Solution Explorer" At the moment it will just show "My Project" and "Form1.Vb"

Now I want you to click once on the form, once it is selected go to the properties menu and look through it until you find "Text|Form1" This is the header of your form, change it to "My First Form!"

Next you need to look in the properties for "Back Color|Control" Click the drop down list and choose any colour you like (You can choose them from the other tabs within the list too!)

Once you have chosen a nice colour you are ready to create and run your first program. Go File, Save All. It should save to a directory similar to this.

C:\Users\andy\Documents\Visual Studio 2010 Express\Projects\

Click save. Now go to Build, Build Hello World, look to the bottom left, it should say "Build Successful"

You are almost done creating your first form, go to the directory you saved the project in, and then go:
Visual Studio 2010 Express\Projects\Hello World\bin\Release

Now run the .exe in the "Release" folder.

Well done, you have created and build your first form, easy huh.

STEP 3: A little bit of Code

Now it is time to make your form do something, I assume you still have VB open? If not go to:

C:\Users\andy\Documents\Visual Studio 2010 Express\Projects\Hello World"

And open the ".vbproj" File.

Now lets get started, double click your form, you should now see your first bit of code.


Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

A little bit confusing? Don't worry, I will explain it.


Code:
Public Class Form1
This will be at the top of all projects (Other than imports, but we will get to that later)
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
This is the "Sub" of your form (nasty functions can be placed in here making a program attack your computer once it is executed), and
Code:
End Sub
This the end of that sub, in between those two bits of code we tell the program what to do when "Form1_Load" Form1 loads.

Lastly.
Code:
End Class
This shows we are done with coding and that it is the end of the program.

Lets start adding some of our own stuff shall we? Click the space below Form1_Load and type (I advise you type, don't C&P)

Code:
MessageBox.Show("Hello World")

What this does it first tell the program we want a message box (MessageBox) Then tells it we want that box to be visible (.Show) Lastly we tell it what we want in the box ( ("Hello World") )

Now I want you to go File, Save All. Then Build, Build Hello World.

At the top you should see there are two tabs, Form1.vb and Form1.vb(Design) Go back to the design one.

Go back to the Release directory and run the .exe again, see what happens when "Form1_Load"s?
 Boom! we now have a windows application that runs a little bit of code we made.


This is all for the painfully noobie material. In the next tutorial we will look at adding code like: if and else statements, Windows STMP mailing (email anyone!), and textboxs and button functionality. (the emailing comes in handy if you want to email yourself .txt logs from our homemade keylogger >:D MOAR TO COME IN LESS THEN A WEEK... )

15 comments:

  1. Looks interesting, I'll be checking back!

    ReplyDelete
  2. Working as a software developer, we use VB for our application but are in the process of switching it over to C#.

    ReplyDelete
  3. Nice, I may have to dust off the old Visual Studios CD-Rom I have lying somewhere. With this information I may be able to code something a little more challenging then a calculator haha. :)

    Oh and thanks for the recommendation about the Stephen Hawking book, I will definitely check it out.

    ReplyDelete
  4. brb going to find and install this program. I really want to learn how to code, baby steps or not, I need to do something!

    ReplyDelete
  5. I really love VB, but I haven't used it in sooo long. This post has enticed me to follow your blog though. It was a helpful post, and if you keep doing cool posts like this, I should be back in the game in no time!

    ReplyDelete
  6. Hi there, very nice tutorial.. I'm learning c++ but I still need to learn classes myself! Hope you will add more later..

    cheers.

    ReplyDelete
  7. Really nicely explained, thanks for the tips!

    ReplyDelete
  8. thank you so much this is so helpful!

    ReplyDelete
  9. Nice man, I'll keep this blog in mind.

    ReplyDelete
  10. Wow i could never figure out how to do that.

    ReplyDelete
  11. i remember learning about VB in high school. man this brings back memories.

    ReplyDelete
  12. Nice post, very informative.

    ReplyDelete
  13. could you change to C++, please?

    ReplyDelete