Introduction to Visual Basic Winforms
In this part of the Visual Basic Winforms programming tutorial, we will introduce the Winforms library and create our first programs using the Visual Basic programming language.
The purpose of this tutorial is to get you started with the Winforms and Visual Basic. Images used in this tutorial can be downloaded here. I used some icons from the tango icons pack of the Gnome project.
About
Windows Forms is a graphical user interface application programming interface (API) included as a part of Microsoft's .NET Framework. As of 13 May 2008, Mono's System.Windows.Forms 2.0 is API complete. Simply put, Winforms is a library for creating GUI applications.
Mono is a cross platform, open source .NET development framework. It is a .NET compatible set of tools, which include C# compiler, Visual Basic compiler, Common Language Runtime, ADO.NET, ASP.NET and Winforms libraries.
vbnc -r:/usr/lib/mono/2.0/System.Windows.Forms.dll quitbutton.vb
The above command shows, how to compile the quitbutton example. The -r parameter of the mono VB compiler loads the Winforms assembly. It is a dynamic library. The commnad shows a path to the dll library on a Ubuntu system.
Centering a window
In the second example, we will center the window on the screen.
' ZetCode Mono Visual Basic Winforms tutorial
'
' This program centers a window
' on the screen
'
' author jan bodnar
' last modified May 2009
' website www.zetcode.com
Imports System.Windows.Forms
Imports System.Drawing
Public Class WinVBApp
Inherits Form
Public Sub New
Me.Text = "Center"
Me.Size = New Size(250, 200)
Me.CenterToScreen
End Sub
Public Shared Sub Main
Application.Run(New WinVBApp)
End Sub
End Class
This code example shows a small window in the center of the screen.
Imports System.Windows.Forms Imports System.Drawing
The Imports keyword imports necessery types that we will use in the application.
Public Class WinVBApp
Inherits Form
In Winforms, any window or a dialog is a Form. This control is a basic container, whose purpose is to display other child controls. Our class inherits from a form. This way it becomes a form itself.
Public Sub New ... End Sub
In the constructor, we set up the application.
Me.Text = "Center"
Here we set a title for the form.
Me.Size = New Size(250, 200)
We set a size for the form.
Me.CenterToScreen
This code line centers a window on the screen.
Public Shared Sub Main
Application.Run(New WinVBApp)
End Sub
We run the example.
Icon
Mono means monkey in Spanish. If we do not provide an icon for our application, we have a head of a monkey by default. The next example shows, how to change this.
' ZetCode Mono Visual Basic Winforms tutorial
'
' This program shows an icon in the
' title bar
'
' author jan bodnar
' last modified May 2009
' website www.zetcode.com
Imports System.Windows.Forms
Imports System.Drawing
Public Class WinVBApp
Inherits Form
Public Sub New
Me.Text = "Icon"
Me.Size = New Size(250, 200)
Try
Icon = New Icon("web.ico")
Catch e As Exception
Console.WriteLine(e.Message)
Environment.Exit(1)
End Try
Me.CenterToScreen
End Sub
Public Shared Sub Main
Application.Run(New WinVBApp)
End Sub
End Class
The code example shows an icon in the upper left corner of the form. A form's icon is the picture that represents the form in the taskbar as well as the icon that is displayed for the control box of the form.
Try
Icon = New Icon("web.ico")
Catch e As Exception
Console.WriteLine(e.Message)
Environment.Exit(1)
End Try
It is a good practice to put all input output work between the Try/Catch keywords. The web.ico file must be available in the current working directory. This is the directory from where we execute (./icon.exe) our application.
Quit button
In the last example of this section, we will create a quit button. When we press this button, the application terminates.
' ZetCode Mono Visual Basic Winforms tutorial
'
' This program creates a quit
' button. When we press the button,
' the application terminates.
'
' author jan bodnar
' last modified May 2009
' website www.zetcode.com
Imports System.Windows.Forms
Imports System.Drawing
Public Class WinVBApp
Inherits Form
Public Sub New
Me.Text = "Quit button"
Me.Size = New Size(250, 200)
Me.InitUI
Me.CenterToScreen
End Sub
Private Sub InitUI
Dim button As New Button
button.Location = New Point(30, 20)
button.Text = "Quit"
button.Parent = Me
AddHandler button.Click, AddressOf Me.OnClick
Me.CenterToScreen
End Sub
Private Sub OnClick(ByVal sender As Object, ByVal e As EventArgs)
Me.Close
End Sub
Public Shared Sub Main
Application.Run(New WinVBApp)
End Sub
End Class
We use the Button. This is a very common widget. It is a rectangular and usually shows a text label.
Me.InitUI
We delegate the creation of the user interface to the InitUI method.
Dim button As New Button button.Location = New Point(30, 20) button.Text = "Quit" button.Parent = Me
We create the button widget. We position it on the form. Provide a label for it and put it inside the form container.
AddHandler button.Click, AddressOf Me.OnClick
When we click on the button, the Click event is triggered. We react to this event with the OnClick method.
Private Sub OnClick(ByVal sender As Object, ByVal e As EventArgs)
Me.Close
End Sub
The OnClick method terminates the application.
This section was an introduction to the Winforms library with the Visual Basic language.