Posted 16 years ago on 6/19/2008 and updated 12/14/2008
Take Away: The ButtonsVB project. Create a classic "Hello, World" application using Visual Studio .Net with VB.Net syntax. Requires either the full version or VB.Net Express Edition.
KB101213
Got 10 minutes? Want to get started with VB.Net Winforms?
This article is part of our series of 10 Minute Quick Starts. Each quick start is step by step, assumes you know very little about the subject, and takes about 10 minutes. You can use them to scratch the service of areas you want to learn and as a quick review when returning to something after a long absence.
VB.Net Winforms
In this tutorial, you will create a classic "Hello, World" windows application called ButtonsVB. Specifically, a form with 3 buttons on it. Each button will display a "Hello, World" dialog but each will demonstrate a different introductory development concept. The goal is to get your feet wet with VS.Net syntax and introduce the Visual Studio development environment. When completed, you can use this ButtonsVB project to test and learn the syntax of VB.Net.
Setup
To get started, you have to have Visual Studio.Net installed. This article is based on VS.Net 2008 but you can use any version you wish (there may be slight variations in menu options but we try to keep our 10 Minute Quick Starts as generic as possible).
If you don't have the full version of Visual Studio .Net, you can download Visual Basic .Net Express Edition:
Start a new project. Open Visual Studio and click File | New Project. Select the Visual Basic > Windows > Windows Forms Application template. In the Name field type ButtonsVB for the project name. Then click OK.
Note - Solutions and projects. All your coding is organized into solutions and projects. A project can consist of a single application (an EXE), website, DLL, class, etc. A solution just brings together a collecton of projects. In this case, we have a project called ButtonsVB in a solution of the same name. Later, we can add another project to this solution if we wish. You can also rename a project but it's easiest to name it now.
Pin the Toolbox. If the Toolbox is not pinned, select it and pin it. If the Toolbox is not pinned, click on the Toolbox tab to display the Toolbox and then click the Tack in the upper right.
Place 3 buttons on the form. Click the Button control then click anywhere on your form (do this two more times). You can also double clock the Button control in the Toolbox to place it on a form. You can quickly align the buttons by clicking and dragging the buttons. You'll see guidelines similar to the blue guidelines in the following screen clip.
Add code to button 1. Double click Button 1 and add the following code:
MsgBox("Hello, World")
Your code window should now look similar to this:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Hello, World")
End Sub
End Class
Note Some developers will refer to MsgBox as the VB way and MessageBox.Show as the .Net way. The "VB Way" is syntax that is compatible or familiar to VB Classic coders and the ".Net Way" was introduced with VS.Net. The benefit of the .Net way is the syntax is generally the same or similar to C# syntax which is a benefit if you ever have to code using C# syntax.
Switching between Code and the Form. The tab with [Design] is the form. In our example, there may be other tabs but the Form1.vb is the code tab and the Form1.vb [Design] is the form.
Compile your code. You are now ready to compile your code into a Windows application. Click Build | Build MyApp where MyApp is the name of your application. If you have any syntax errors or compiler warnings, this is when Visual Studio will let you know and you can fix them.
Run-it! Now let's test your very first program. You can run it within Visual Studio (called debugging) or you can run the EXE outside of Visual Studio. To runt it you start a debugging session. To start a debugging session, either click the toolbar debug icon, select Debug | Start Debugging, or press F5. Click Button 1 to show the Hello, World dialog. Click OK then close the form to abort the debugging session.
You can also run the application outside of Visual Studio. The compiled EXE is located in your debug folder. Here is the location of mine:
D:\MP\Documents\Visual Studio 2008\Projects\WindowsApplication8\WindowsApplication8\bin\Debug\WindowsApplication8.exe
You can even distribute your EXE to any Windows computer with the appropriate DotNet Framework installed. The DotNet Framework is required because a DotNet application is not a true native code application like the ones created with tools such as C++ and Delphi.
Add code to Button 2. The MsgBox function is a VB.Net syntax function similar to the MsgBox function in Visual Basic. It wraps up the base MessageBox.Show code so if you're going to be coding in both VB.Net and C#, you might wish to use the MessageBox equivalent. Add the following code to Button 2's click event:
MessageBox.Show("Hello, World")
Add code to Button 3. So far we've been using literals for the text in our dialog box. This time let's declare two variables, concatenate them together and use the variable with the MessageBox member. For good measure, we'll make use of both types of single line comments too (VB.Net does not have a multi-line comment).
Dim FirstName As String
Dim LastName As String
Dim FullName As String
'Assign variables.
FirstName = "Nathan"
LastName = "Prestwood"
FullName = FirstName + " " + LastName
'Display dialog.
MessageBox.Show("My son's name is " + FullName)
REM This is also a comment but the old basic REM comment command is rarely used any more.
Test-It!
Part Two: The Help System
The .Net Framework Library. Good development environments provide a library of prewritten code for your use. For example, Borland Delphi provided it's Visual Component Library (VCL). The .Net framework class library is a very rich set of routines ready for you to use. The better you know the framework, the easier coding in DotNet will be. Since the framework is a class library that fully exploits and abides by the tenets of object oriented coding, the more you study general Object Orientation (OO) the better.
The Help System. Each development environment has it's way of documenting it's own code library, event model, and tool usage. Each time you use something new in Visual Studio, breifly read about it in the online help system.
Click on the MessageBox command we typed above to put the cursor within it. Then press F1 to launch the help system. Notice under reference the name space that contains MessageBox is listed including the namespace path.
The following screen clip is VS.Net 2008:
Namespace path is optional. Above we typed just the MessageBox.Show command but you can actually specify the full namespace path if you wish. For example, alter our code above to include the full path as follows:
System.Windows.Forms.MessageBox.Show("My son's name is " + FullName)
Intellisense. The editor helps you in many ways including Intellisense which pops up help as you type. To see this in action within the contect of the namespace we are exploring, type the above command on a new line.
What now?
Now that you've gotten your feet wet with VB.Net, you can start learning how to do specific coding tasks, learn the intricies of the language, and the ins and outs of the development environment. A good next step is to browse the varioius menu options and see what they do. Select various menu options, when in the feature, press F1 for a quick overview.
If you want a bit more syntax, click on the related post below.
That's it! I hope this was helpful.
In this tutorial, you will create a classic "Hello, World" windows application. Specifically, a form with 3 buttons on it. Each button will display a "Hello, World" dialog but each will demonstrate a different introductory development concept. The goal is to get your feet wet with VS.Net syntax and introduce the Visual Studio development environment.
This article is part of our series of 10 Minute Quick Starts. Each quick start is step by step, assumes you know very little about the subject, and takes about 10 minutes. You can use them to scratch the service of areas you want to learn and as a quick review when returning to something after a long absence.