

Navigator : Home > Tutorials > Windows Programming Tutorials > ...
Introduction to Windows Forms 2 in VB
In this tutorial we will look at how to create a Windows Form and how we can dynamically change it's appearance through the code-behind, as well as look at creating a custom Exit Application button, and message boxes. VB version.
This tutorial will serve as an introduction to programming for the Windows platform. In this tutorial we will learn such basic things as how to create a Windows Form and how to dynamically change the Header Title and dimensions of the form and the background color, as well as how to make use of message boxes and how to create an exit application button.
When working in the code-behind of the form (Form1), we can reference the form itself simply using the keyword Me. For example, we will resize the form when it is loading with the following code:
| Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "Change form title here"
Me.StartPosition = FormStartPosition.CenterScreen Me.AutoScaleBaseSize = New Size(5, 13) Me.ClientSize = New Size(400, 200) Me.MaximizeBox = False End Sub |
The first line of this code simply sets the Title of the Form. The other four lines of code set the position and dimensions of the Form.
Next, we will add functionality to detect when the user clicks anywhere on the Form, and tell them. First, we add the following in the Form Load event:
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
| AddHandler Click, AddressOf ClickForm |
Then we add the ClickForm Event Handler:
| Public Sub ClickForm(ByVal ob As Object, ByVal e As EventArgs)
MessageBox.Show("You clicked on Form Area", "Message box Title") Me.BackColor = Color.Red End Sub |
Finally, we will create a friendly Exit button for the user. And before the application exits, we will notify the user.
We drag a button onto the Form Design view from the toolbox, then double click it to create a handler:
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("This program will now close.", "EXIT") Application.Exit() End Sub |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
The entire code-behind will look something like this:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "Change form title here"
Me.StartPosition = FormStartPosition.CenterScreen Me.AutoScaleBaseSize = New Size(5, 13) Me.ClientSize = New Size(400, 200) Me.MaximizeBox = False
AddHandler Click, AddressOf ClickForm End Sub Public Sub ClickForm(ByVal ob As Object, ByVal e As EventArgs)
MessageBox.Show("You clicked on Form Area", "Message box Title") Me.BackColor = Color.Red End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("This program will now close.", "EXIT") Application.Exit() End Sub End Class |
Looking for the C#.NET 2005 Version? Click Here!
Looking for more .NET Tutorials? Click Here!