VB.NET 1.1 Tutorial - Event Driven Programming - Part 1


In this example, two objects on a form interact when a button is pressed. The text that is entered into the textbox is displayed in the label when the button is pressed.

Figure 9-1 - Windows Application with paint event

The application consists of a button, a textbox and two labels

Listing 9-1 - Event Driven Application

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
  Inherits Form

  Public Sub New()
    MyBase.New()

    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  Private components As System.ComponentModel.IContainer

  Friend WithEvents Label1 As Label
  Friend WithEvents TextBox1 As TextBox
  Friend WithEvents Button1 As Button
  Friend WithEvents Label2 As Label

  Private Sub InitializeComponent()
    Me.Label1 = New Label
    Me.TextBox1 = New TextBox
    Me.Button1 = New Button
    Me.Label2 = New Label

    '
    'Label1
    '
    Me.Label1.Location = New Point(40, 40)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New Size(100, 20)
    Me.Label1.TabIndex = 0
    Me.Label1.Text = "Enter Your Name : "
    '
    'TextBox1
    '
    Me.TextBox1.Location = New Point(140, 38)
    Me.TextBox1.Name = "TextBox1"
    Me.TextBox1.TabIndex = 0
    Me.TextBox1.Text = ""
    '
    'Button1
    '
    Me.Button1.Location = New Point(140, 80)
    Me.Button1.Name = "Button1"
    Me.Button1.Size = New Size(70, 20)
    Me.Button1.TabIndex = 1
    Me.Button1.Text = "Click Me"
    '
    'Label2
    '
    Me.Label2.Location = New Point(40, 120)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New Size(250, 20)
    Me.Label2.TabIndex = 0
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New Size(5, 13)
    Me.ClientSize = New Size(300, 180)
    Me.Controls.Add(Me.Label2)
    Me.Controls.Add(Me.Button1)
    Me.Controls.Add(Me.TextBox1)
    Me.Controls.Add(Me.Label1)
    Me.Name = "Form1"
    Me.Text = "Event Driven Form"
  End Sub

  'Run the application
  <STAThread()> Public Shared Sub Main()
    Application.Run(New Form1( ))
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label2.Text = "Thanks a Lot " + TextBox1.Text
  End Sub
End Class 

Copy and paste the text into notepad and save the file as eventform.vb. Alternatively, you can download the file eventform.vb from the link.

Now open a Command Line in the directory where you saved the file and type the following. Alternatively, you can download my batch file here.

vbc /t:winexe /r:system.dll,system.drawing.dll,system.windows.forms.dll eventform.vb

Close the command line and run the eventform.exe file just like any other executable in windows. You can download my copy of the executable from here. If you have Visual Studio, you can download the solution here.

We have already discussed adding a label previously in Hello Mum - Windows Style - Part 2. I will briefly discuss the other two components here.

Adding a Button

Instead of declaring the components as Private, this time they have been declared using the access modifier Friend.

Friend WithEvents Button1 As Button

An object that can raise events should be declared with the WithEvents keyword.

The button is instantiated and setup in the InitializeComponent method in a very similar method to how we setup the label as previously mentioned.

Me.Button1 = New Button

. . .

'
'Button1
'
Me.Button1.Location = New Point(140, 80)
Me.Button1.Name = "Button1"
Me.Button1.Size = New Size(70, 20)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Click Me"

. . .

Me.Controls.Add(Me.Button1)

Finally we need the button to be able to do something when it is clicked. So we add a method to do this. Note that unlike C# we do not need to add an event handler, the compiler takes care of this for us.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Label2.Text = "Thanks a Lot " + TextBox1.Text
End Sub

Adding a TextBox

In the buttons click event, we take the text from a TextBox and add it to a Label. The TextBox is declared and instantiated in the same manner as the button, but we do not add any events to it.


References

For more information on the Button class, visit MSDN at microsoft here.

For more information on the Control.Click Event, visit MSDN at microsoft here.

For more information on the Label class, visit MSDN at microsoft here.

For more information on the TextBox class, visit MSDN at microsoft here.

What Next?

Next we will look at GDI+.

Return to the Tutorial Contents.