Creating a Visual Basic application from the Command Line

This walkthrough assumes that you have installed the .NET Framework SDK and that you have some familiarity with console (DOS) windows. If you have a favorite text editing program you can use it to follow along, but all the examples will assume you are using Notepad.exe.   


Creating a Console Application

Console applications are run from the command line, similar to the traditional DOS commands you might already be familar with (dir, cd, copy, format). They are new to Visual Basic and not as common as Windows Applications, but since you are working from the command line to write the code they are a great place to start.

Step 1. Adding the Framework SDK to your PATH

To use the command line compiler, vbc.exe, it has to be available somewhere in your PATH (the set of directories that are searched for applications when you are working at the command line). The simplest way to set this up is to work through Windows, by going into the System control panel and clicking the Environment Variables button.

Then select the PATH entry and add the following text after the end of what is already in place;

c:\windows\microsoft.net\framework\v1.1.4322

Note: replace 'c:\windows\' with the path of your Windows directory...

With these changes in place, you can now use the Visual Basic command line compiler (vbc.exe) and other .NET Framework tools from any command prompt.

Step 2. Creating the code file

Create a directory somewhere on your computer, 'c:\firstapp' would do just fine. Now open your text editor (notepad will do) and enter the following lines of code;

Module Main
    Sub Main()
        System.Console.WriteLine("Hello!!")
        System.Console.ReadLine()	
    End Sub
End Module

Save this file as helloworld.vb, into the directory you created earlier (I saved mine into c:\firstapp\helloworld.vb).

Step 3. Compiling your application

Open up a console window (click Run on the Start Menu and type 'cmd' then click OK), and navigate to your code directory (so, for me... I typed 'cd \' to get to the root of my drive, then 'cd firstapp' to get into the directory I had created). Once you are in that directory, you can type;

vbc helloworld.vb

when you press enter, the Visual Basic compiler will turn your code (helloworld.vb) into an executable (helloworld.exe).

Step 4. Testing your new program

To try out your new program, just type
helloworld
and press enter.

You'll see a nice little greeting and then the program will wait for you to press enter before exiting.

Ta da... you've greated your first console application!!

Creating a Windows Application from the Command Line

The process for creating a windows application from the command line isn't all that different than the steps you just went through for creating a console application, but there are a few new options you will have to set when you compile your code and, of course, the code that you write will be quite different.

 

Step 1. Creating the code file

You set up the PATH information and created a code directory already while building the console application, so for this walk through you can go right to creating the .vb file full of code. Enter the following lines of code into an empty text file.

Imports System
Imports System.Windows.Forms

Public Class myForm
        Inherits Form
    Dim WithEvents myButton as Button

    Public Sub New()

        myButton = new Button()
        myButton.Text = "Click me!"
        myButton.Top = 50
        myButton.Left = 50

        me.controls.add(myButton)

    End Sub

    Private Sub myButtonClick(sender as Object, e as EventArgs) _
		Handles myButton.Click
        MessageBox.Show("Hey, nice app!!")
    End Sub

End Class

Now save that as a .vb file, named 'winapp.vb' in my example, and then switch back to the console window from the previous section or open a new one.

Step 2: Compiling your Windows Application

Navigate to your code directory if you aren't already there and compile your new program with;
vbc winapp.vb
You'll see a whole bunch of errors, but the code is all correct, something needs to be done differently to make this work. What is happening in this case is that several things have changed;

To handle each of these issues, we need to pass additional information to the command line compiler. For the additional libraries, we will add references by using the /r: command line switch.

/r:system.windows.forms.dll /r:system.dll

To inform the compiler that we are building a Windows application, we need to specify a 'target' switch on the command line.

/t:winexe

And, finally, to tell the compiler that our new Form (myForm) should be the starting point of the application, we need to add another switch;

/m:myForm

Putting it all together we get

vbc winapp.vb /r:system.windows.forms.dll /r:system.dll /t:winexe /m:myForm

Which will produce an executable, winapp.exe, that you can run just be typing winapp (and pressing enter).