Creating Visual Basic Applications from the Command Line

Creating Visual Basic Applications from the Command Line

2629
0
SHARE
Creating Visual Basic Applications from the Command Line

One of the most exciting aspects of Visual Basic has always been the ease and speed with which programmers may create Windows applications. The grammar used in Visual Basic coding is very similar to English and the structure of the program is clean and easy to follow. And now that Visual Basic is incorporated into the Microsoft .NET framework it has become very simple for anyone to create a Windows form, even if they only have Notepad as their development environment.

Creating a Simple Windows Application in Notepad

Every Windows computer in the world has the Notepad text editor and the new programmer needs not more than that to start coding. And, of course, the programmer does not need to understand the intricacies of displaying forms on the screen, they just need to know which Windows namespaces (which are groups of classes) to load:

Imports System.Drawing
Imports System.Windows.Forms
These two namespaces will enable the developer to create a form and position it on the PC’s desktop. The form to be created by the programmer is a class that inherits all of the methods and properties of the default Windows form:

Public Class Form1
Inherits Form
At this only on method is required by the class. The “New” method is run when the class is instantiated:

Public Sub New
Me.Location = New Point (100, 100)
End Sub
End Class
All that’s required now is the code that will create the form itself:

Module Module1
Shared Sub Main
Application.Run (New Form1)
End Sub
End Module
The next stage is to save the file (for example to C:\vb\simple_form.vb), compile it and then to run the application.

Compiling and Running the Windows Form

Before going any further the programmer must ensure that they have the Microsoft .NET framework installed. That’s for one very important reason – it comes with the Visual basic compiler (vbc.exe). If the location of the compiler (for example: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727) is added to the path the the programmer may open a command prompt, move to the folder containing the new VB file and then type:

vbc simple_form.vb
It can then be run by typing:

simple_form
The end result can be seen in figure 1 at the bottom of this article.

Adding Components to the Form

The programmer can now extend the form and add components such as buttons and text boxes. To do this they will need to import an additional namespace:

Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
This namespace gives the programmers classes for buttons and text boxes:

Public Class Form1
Inherits Form
Public WithEvents button1 As Button
Public WithEvents text1 As TextBox
And these can then be added to the form:

Public Sub New
Me.Location = New Point (100, 100)
button1 = New Button
button1.Size = New Size(40, 20)
button1.Location = New Point(30, 30)
button1.Text = “Click me”
Me.Controls.Add(button1)
text1 = New TextBox
text1.Location = New Point(30, 70)
Me.Controls.Add(text1)
End Sub
The form now has some components (as can bee seen in figure 2), but no actual functionality.

Adding Functionality to the Form

Finally the class requires a subroutine that handles the button click event (which fires when the user clicks the button on the form):

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles button1.Click
Me.text1.text = “Hello World”
End Sub
End Class
In this example the text box is populated when the user clicks the button. It is a simple example but shows just how easily any Windows user can produce a working form.