Wednesday, January 11, 2012

Beginner Tutorial - Hello World

Visual Basic is a great starter programming language. Not only is it easy to learn, but many business applications use it extensively for their applications. If you are just starting to learn to develop applications - this is a great language to start with. This tutorial and the many other Visual Basic tutorials at this site will give you a solid foundation in your Visual Basic knowledge.

Lets jump right in with a simple application. When you are done with this Visual Basic tutorial, you should have a complete working application that when you click on the button it will say "Hello, World"

Start Microsoft Visual Basic 6.0 (VB6)
The New Project dialog box will appear. If it doesn't go up to the menu bar and select File -> New Project


In the New Project dialog select Standard EXE, and click the Open Button.

This will bring up your new Project 1 application with Form1 visible.

Already Visual Basic has done a lot for us. As you can see this tutorial isn't very long but already you have a full working application. You can see your new program in action by going up to the menu bar and selecting Run -> Start (Or simply press the F5 key).

You should see the Form1 window appear:


This is a fully functional application. You can move it around, minimize and maximize it, and close it down. For you to do this same thing in C++ - the original language most of Windows was written in you would have written hundreds of lines of code. You area already getting to see some of the extreme power VB gives you. Now lets continue with the tutorial.

Lets make this program say hello!


On the left side of the screen you can see the toolbox (if this doesn't show up go to the top menu bar and select View -> Toolbox). In this toolbox you will see a picture of a button. Double click the button icon and it will create a Command1 CommandButton in the center of your form.


If you run the program now (Press F5) you will see your window now has a button labeled Command1 in the center of it, but if you click the button it doesn't do anything. So lets wire things up so our program will say "Hello, World" when you click the button. Close out of your running program so you are back to the main design environment (pictured above)

Visual Basic allows you to do event driven programming. This is a concept that is very powerful and easy to use. Event driven programming works as follows: Visual Basic has many different events defined that occur when a specified thing happens. We as the programmer can link into these events and write our custom code to do what we want. One very useful event is the Click event. This event occurs any time the user clicks on the specified object. I'm sure the wheels are already turning in your head, if we want to say hello world when someone clicks the button on our form than we should do something in the Click event for the Command1 button. That is exactly what we are going to do.

To write the click event code double click on the Command1 button. This will bring up the code editor and will automatically write the beginning code to handle the click event.


Now any code you put between the Private Sub ... and the End Sub statements will be executed when the user clicks the command button. To demonstrate this we will have a message box appear saying hello world. So add this line of code:

Private Sub Command1_Click()
     MsgBox "Hello, World!"
End Sub

MsgBox is a built in Visual Basic function that causes a message box to be displayed to the user. The first parameter this function takes is the text string you wish to have displayed. We choose the text string "Hello, World!". MsgBox also takes other parameters to specify things such as what buttons to display and what caption to use, but these will be discussed later. For now lets see how this works. Run your newly created Visual Basic hello world program (Press F5). Once the program is running click the Command1 button, you should see a message box saying Hello, World! appear.

Congragulations! You have written a complete working Visual Basic program using this Hello, World tutorial. Read some of our other VB6 tutorials to learn how powerful this language really is.

credit: vb.us

0 comments:

Post a Comment