Windows Application in VS.NET using VB.NET

In this article I will explain you about Creating a Windows Application in VS.NET using VB.
  • 2220

Now that you have seen how to create a Windows application from the command line, let's create a Windows application using the VS.NET wizard.

Creating a Skeleton of the Application

Select New->Project->Visual Basic Projects->Windows Application from your VS .NET IDE.

Creating a Windows Application Project


Press the OK button. The IDE takes you to the Design view of a form as in below image.

Design View of a Windows Application.


If the Solution Explorer is not visible, it can be opened from the View menu. The Solution Explorer (see Figure 9.5) provides a list of all available files in your project. The Form1.vb file contains the code for the form and its controls.

Available Classes in a Windows Application Project


To view the code added by the wizard, right-click on Form1.vb and select the View Code menu option as illustrated in below image.

View Code Option


The code added by the wizard is discussed in the "Reviewing the Code" section.

Adding Controls

The next step is to add controls to the form. To open the toolbox, choose the View->ToolBox menu item as shown in below image.

Toolbox Option


The Toolbox


The toolbox looks like that in Figure 9.8. To add controls to the form, drag a control from the toolbox onto the form and position it anywhere you want. Drag two Button controls and a TextBox, a Label, and a CheckBox control to the form and adjust them so that the form is similar to that shown in below image.

Controls on the form in design view


Set the properties of these controls by right-clicking the control and selecting the Properties menu option. The Properties window, shown in Figure 9.10, permits a developer to set design-time properties from within VS.NET. The property list varies, as would be expected, depending on the control type. The design-time values can be considered a control's default values, as they can be changed anytime during the program's execution. A property's value may be changed in two locations. If a value on the property page is altered, that change is reflected in the InitializeComponent method's code, and vice versa a property's value modified in the code is mirrored on the property page by VS.NET.

Properties Window


Set button1's Text property to Set Color and button2's property to Set Font.

Reviewing the Code

Now let's review the code that the Form Designer has written. You can view the code by rightclicking Form1.vb and choosing View Code. The code generated by the wizard appears in Listing 9.3. An instance of the form is created in the Main method as a parameter in the call to Application.Run().

Listing A : Code Added by the Wizard for the Windows Application

Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Namespace Listing9_3App
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Private button1 As System.Windows.Forms.Button
        Private button2 As System.Windows.Forms.Button
        Private textBox1 As System.Windows.Forms.TextBox
        Private label1 As System.Windows.Forms.Label
        Private checkBox1 As System.Windows.Forms.CheckBox
        Private components As System.ComponentModel.Container = Nothing
        Public Sub New()
        End Sub
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If components IsNot Nothing Then
                End If
            End If
        End Sub
"Windows Form Designer generated code"

        Private Sub InitializeComponent()
            Me.button1 = New System.Windows.Forms.Button()
            Me.button2 = New System.Windows.Forms.Button()
            Me.textBox1 = New System.Windows.Forms.TextBox()
            Me.label1 = New System.Windows.Forms.Label()
            Me.checkBox1 = New System.Windows.Forms.CheckBox()
            ' button1
            Me.button1.Location = New System.Drawing.Point(24, 24)
            Me.button1.Name = "button1"
            Me.button1.Size = New System.Drawing.Size(112, 40)
            Me.button1.TabIndex = 0
            Me.button1.Text = "Set Color"
            ' button2
            Me.button2.Location = New System.Drawing.Point(160, 24)
            Me.button2.Name = "button2"
            Me.button2.Size = New System.Drawing.Size(120, 40)
            Me.button2.TabIndex = 1
            Me.button2.Text = "Set Font"
            ' textBox1
            Me.textBox1.Location = New System.Drawing.Point(32, 104)
            Me.textBox1.Name = "textBox1"
            Me.textBox1.Size = New System.Drawing.Size(104, 20)
            Me.textBox1.TabIndex = 2
            Me.textBox1.Text = "textBox1"
            ' label1
            Me.label1.Location = New System.Drawing.Point(176, 104)
            Me.label1.Name = "label1"
            Me.label1.Size = New System.Drawing.Size(120, 24)
            Me.label1.TabIndex = 3
            Me.label1.Text = "label1"
            ' checkBox1
            Me.checkBox1.Location = New System.Drawing.Point(120, 168)
            Me.checkBox1.Name = "checkBox1"
            Me.checkBox1.Size = New System.Drawing.Size(176, 40)
            Me.checkBox1.TabIndex = 4
            Me.checkBox1.Text = "checkBox1"
            ' Form1
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(376, 273)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.checkBox1, Me.label1, Me.textBox1, Me.button2,
            Me.Name = "Form1"
            Me.Text = "Form1"
        End Sub
#End Region
        <STAThread()> _
         Shared Sub Main()
            Application.Run(New Form1())
        End Sub
    End Class
End Namespace

The wizard creates a default namespace, Listing9_3App, that bears the same name as the project. It also adds references to various namespaces required by Windows Forms. Note that the Form1 class is derived from System.Windows.Forms.Form. The Dispose method performs any required cleanup of the resources and is called by the runtime when the application closes. The InitializeComponent method creates the Form and all its child controls. In Listing 9.3, the wizard sets the essential control properties and adds the controls to the form via the Controls.AddRange method.

The SuspendLayout and ResumeLayout methods, in the InitializeComponet method, should not be tampered with without a good reason. As the names imply, the methods suspend and resume a layout event, which is triggered whenever a child control must be repositioned or resized. If the layout events are not suspended during the initial window construction, the unnecessary number of events drastically slows the window's creation.

Adding an Event Handler

The last part of this tutorial involves adding an event handler for the Set Font and Set Color buttons. To create an event handler for a button, double-click the button. This generates the required code for a "click" event. Using this method for wiring up an event, the event handler method name will contain same name as the name of the button object. An alternative method is to use the control's Properties window and click the lightning bolt button as demonstrated in below image.

Adding an Event Handler for a Button


In above image, the button1_Click method is the event handler for that button. If the control is deleted after creating an event handler, the handler method must be manually removed.

Next we create an event handler for button2. Listing 9.4 shows the button1 and button2 click event handlers' code. The button1_Click method uses the ColorDialog class to select a color and set the colors for all controls. The button2_Click method uses the FontDialog class to set the font of all the controls on the form.

Button Click Event Handler for Set Font and Set Color Buttons
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim colorDlg As New ColorDialog()
    textBox1.BackColor = colorDlg.Color
    label1.BackColor = colorDlg.Color
    checkBox1.BackColor = colorDlg.Color
End Sub
Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim fntDlg As New FontDialog()
    fntDlg.ShowColor = True
    If fntDlg.ShowDialog() <> DialogResult.Cancel Then
        textBox1.Font = fntDlg.Font
        textBox1.ForeColor = fntDlg.Color
        label1.Font = fntDlg.Font
        label1.ForeColor = fntDlg.Color
        checkBox1.Font = fntDlg.Font
        checkBox1.ForeColor = fntDlg.Color
    End If
End Sub

Setting Properties at Runtime

We have just shown how to set a control's properties from the Properties window. You can also set the properties programmatically. For example, consider a button's color properties. The background and foreground colors of controls can be changed by using the BackColor and ForeColor properties. The same method for altering color applies to all control properties. They can be set both at designtime and runtime.

        button1.BackColor = System.Drawing.Color.Blue

The Font property of a control allows you to change the font of that control. The Font class of the System.Drawing namespace is used to create a new font. The following code demonstrates how to change the font of a button. Again, the same method for changing fonts applies to all controls.

        button2.Font = New System.Drawing.Font ("Verdana", 10, System.Drawing.FontStyle.Bold)

Building and Running the Project

To run the program press CTRL+F5 or click Start Without Debugging on the Debug menu as shown in below image

Building a Project


Now, run the project and click the Set Color and Set Font buttons. The Set Color button click sets the color of the controls, and the Set Font button sets the font of the controls. The Set Color button calls the ColorDialog class and lets you select a color, as you can see in below image.

ColorDialog Dialog Box


The Set Font button event click calls the FontDialog class (see Figure 9.14) and allows the selection of a font as well as its color, style, and size.

FontDialog Dialog Box


Above figure shows the application after the color and font have been set.

Windows Application Output After Setting Color and Font



Hope this article would have helped you in understanding Creating a Windows Application in VS.NET using VB.


More Articles

© 2020 DotNetHeaven. All rights reserved.