Saying Hello to Visual Basic 2005! - dummies

Saying Hello to Visual Basic 2005!

By Bill Sempf

In this article, you’ll get started with the classic Hello World program. Although this isn’t the single most exciting application you can build, it helps to make sure that your development environment is set up the best way possible.

Setting up Visual Studio

To follow this example, you need to start by running Visual Studio 2005, which is a development environment to build applications in Visual Basic. Before you can run Visual Studio, you need to install it!

Visual Studio comes in a number of editions:

  • Team System: Designed for full programming staffs in large corporations, this edition includes large-scale application system design tools such as test-driven development and Team Foundation Server.
  • Professional Edition: Designed for the developers working with users in a standalone setting. The Professional Edition is more common for the solo developer or for mid-sized application development.
  • Standard Edition: Designed for building smaller, standalone applications, this version is perfectly functional for 80 percent of applications built. But if you plan to build large systems that need to be enterprise-quality and may have many users, go for the Professional Edition.
  • Express Edition: Designed for students and hobbyists. This version lacks a lot of the project types that the other versions have.

If you don’t have access to the MSDN Library (Microsoft’s handy technical archive), get it. You can load up a machine with your choice of sample code, documentation, and other reference material on Visual Studio editions, operating systems, and server software. You can find out about the Microsoft library, and you can buy subscriptions from several resellers, including your favorite software dealer.

Installing Visual Studio can be rough, so go with the defaults for your first time. The installation process takes quite a while, too. Even if you are using the DVD, expect to spend two hours installing. If you are working from the CDs, expect to spend four hours.

After installing Visual Studio, you can run it by choosing Start –> All Programs –> Microsoft Visual Studio 2005 –> Microsoft Visual Studio 2005. The environment loads, and you can get started on a program by choosing File –> New –> Project from the main menu. Next, you need to make choices about your project type and language, as described in the next section.

Starting a Windows Forms project

After you choose File –> New –> Project from the Visual Studio main menu, the New Project dialog box appears. In the Project Types pane, you find a folder structure that lists the languages loaded with your installation and the project types available for those languages. Begin with a plain old Windows Application — which is the Visual Basic 2005 answer to the traditional (and perhaps familiar) VB 6.0 application.

To get started building your Hello World application, following these steps:

1. Select the project type from the Templates pane in the New Project dialog box.

2. Type the name you want to give your project to replace the default name in the Name text box.

3. Click OK.

Visual Basic loads the default form (called Form1) and presents it to you in the Design View. The default form comes complete with a workspace, the title bar, and familiar windows elements like the resize buttons and the Close button. You do most of the work to customize your form using this visual view.

4. Click the word Toolbox on the left side of the screen.

The Toolbox appears, with Windows Forms controls loaded.

5. Double-click the Button control.

Visual Studio loads a button onto the default form in Design View.

6. On the default Form1, click the Button control and drag it to reposition it on the form.

This step list gives you the beginnings of the Windows Forms application, which you see as a Form1 in the Design View. But to see where Visual Basic comes in, you have to find the code behind the form. Visual Studio offers you (surprise!) the Code View when you’re ready to use Visual Basic to add functionality to your form.

Adding functionality to the form with VB code

To add a little functionality to the Windows form you build in the preceding section, follow these steps:

1. Double-click the Button control to enter Code View.

In the Code View window, you see basic button-click code that looks like the following:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button1.Click

End Sub

End Class
This code is a template that wraps the code that will be run when you click the button. Visual Studio does the hard part for you, making sure the formatting of the Sub is correct!

2. In the Code View window, type a line of code to change the text that appears on the Button control to Hello World.

Specifically, type the following code on the line preceding the End Sub line:

Button1.Text = “Hello World”
Your button’s code now looks like the following:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button1.Click

Button1.Text = “Hello World”

End Sub

End Class

Running and operating your Windows form

So, this experience is pretty cool, right? Programming with Visual Basic is so easy that you can already write a Windows Forms application. But what can you do with it? Check out the following:

  • Run your Windows Forms application within the Visual Studio environment. Press the F5 key on your keyboard, and Visual Studio opens your active project as a Windows program. It appears in your taskbar and everything. Click the button on your form, and the button text changes to “Hello World,” (or whatever text you specified in the code). Pretty neat, huh?tabmark
  • Run your application outside of the Visual Studio environment. If you are still in Debug mode, you will need to stop your program first by using the Stop button on the toolbar or by closing the form window. Then you can save and move on.
    The very simple way to run an application outside of Visual Studio is as follows:

• 1. Choose File –> Save All from the Visual Studio main menu.

• The Save Project dialog box appears, and Visual Studio prompts you to pick a location to save your project. In this case, accept the default folder.

• 2. Click the Save button.

• 3. Choose Build –> Build Program Name from the main menu.

• In this example, choose Build –> Build HelloWorld, and Visual Studio compiles your application into a usable Windows program (with file extension .exe) and stores it in the default folder.

• 4. Navigate to the default folder containing your new Windows application.

• 5. Double-click the filename for the compiled program to run it.

• You may see a host of files in the default folder, but in the example, Hello World.exe is the file you’re looking for.

There is a more complex method for running your VB programs outside the Visual Studio environment. You use a Setup Project, which is a very cool tool but beyond the scope of this book. Research the term Setup Project in the MSDN Library when you’re ready to find out more about this device, which helps you distribute your application to other users.