Beginning VB.NET - Hello World

Level:
Level1

So you want to learn how to program in VB.NET. The best way to start learning is by jumping in. Follow the instructions on this page and within the next few minutes you will have your first Windows Application saying Hello to you. Its fun, easy, and very addicting as you being to learn the ins and outs of Visual Basic .NET.

Start by running Microsoft Visual Basic 2010 Express Edition (Or Visual Studio if you’ve purchased the full version). Do this by double clicking the desktop icon or through your Start Menu.

clip_image002

If this is your first time running it, VB 2010 will take a little while to startup as its configuring everything for first time use. Sit back and as the screen says be patient Winking smile.

Once everything starts up you should see a screen similar to this:

clip_image004

Lets create a simple application.

  • First goto File -> New Project.
  • In the New Project dialog Select Windows Form Application and at the bottom of the dialog type in a name for your project (such as HelloWorld). Then click the Ok button.

You should now see a screen similar to this:

clip_image006

If you run this program right now you would see the Form1 screen appear and nothing else. Lets add a button and have our program say hello when the user clicks on it. To Add the button click on the Toolbox on the left side of the screen (where the red circle is above).

The toolbox window will popup. Next expand the Common Controls tree and double click on the Button choice. See this picture as an example:

clip_image008

By double clicking on the Button choice in the toolbox you are telling the VB.NET IDE to add a button to your main form using the default size and default location. This works great for our Hello, World application but in the future you will often want to simply click once on the button choice in the toolbox and then draw the button by hand on your form.

If you look on the Form1 form you will see that a button has been added in the upper left corner of the form. It is labeled “Button1”. Grab that button and move it to the center of the form. Now if you look over on the right side of the screen you will see a properties window. Lets change the (Name) of our button from Button1 to mainButton. Also change the text property to Click Me!. Once you have made these changes your screen should look similar to this:

clip_image010

So far all we’ve been doing is setting up the User Interface to our application. The nice thing about VB.NET is that you can do this so easily. Behind the scenes the Visual Basic .NET IDE is automatically generating your code. Most people would not consider what we’ve done so far as actual programming, although this point could be debated.

However, what we are going to do next is definitely programming. Lets write some code to make our application say hello.

Double click on the Click Me button you’ve just changed the properties of. By double clicking you’ve just used a built in shortcut to do something very interesting. You have told the VB.NET IDE that you want to write code for an event – specifically the click event for our mainButton. You should see the code like this:

Public Class Form1
    Private Sub mainButton_Click(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) Handles mainButton.Click
 
    End Sub
End Class

Now any code you add between the Private Sub mainButton_Click(…. Line and the End Sub line will be executed when the user clicks on the button. Lets add a line of code there that will say hello. Add the line of code:

MessageBox.Show("Hello, World!")

This command is a called a function call. We are interfacing with the built in MessageBox class and telling it to Show a default message box with the phrase Hello, World in it. We put double quotes around the Hello, World phrase because this tells the VB.NET compiler that this is a string. A string is any set of multiple characters. You put them in double quotes because otherwise the VB.NET compiler thinks it’s a command that it doesn’t understand and it will get upset. Note: If you come from a Visual Basic background you might be used to displaying a Message Box using the msgbox function. You can still do this in VB.NET but it is only there to hold your hand. Instead I recommend you get used to doing things the .NET way and use the MessageBox.Show() function.

After adding this line your code should look something like this:

Public Class Form1
    Private Sub mainButton_Click(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) Handles mainButton.Click
        MessageBox.Show("Hello, World!")
    End Sub
End Class

That’s it! Your first program is complete. You can see it in action by going to the top menu bar and selecting Debug -> Start Debugging or you can use the shortcut key F5. You will be doing this often when programming in Visual Basic. NET so I suggest you start to learn the shortcut keys now. Press F5 and you should see your form appear on the screen. Click the Click Me button and your program should say hello to you.

clip_image012

Congratulations you’ve just created your first VB.NET application. This probably seemed very simple, and it is. That’s the beauty of VB.NET. If you were to write this same application using C++ you would have to write hundreds of lines of code. VB.NET allows you to instead create your applications in a rapid matter – they call this RAD (Rapid Application Development).

If you would like to see the source code for this in action you can download it here:

If you enjoyed this post, subscribe for updates (it's free)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.