VB.NET 1.1 Tutorial - Hello Mum - Windows Style - Part 4


In the last section we overrode the OnPaint method to display "Hello Mum!" on the form. This time we shall provide our own paint event.

Below is the complete listing of the new program.

Listing 6-1 - Hello Mum Windows Application

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

Public Class HelloPaint
  Inherits Form

  'Run the application
   Public Shared Sub Main()
    Application.Run( New HelloPaint() )
  End Sub

  Public Sub New()
    MyBase.New()
    Me.Text = "Windows Forms Demo"
    Me.ClientSize = New System.Drawing.Size(300, 200)
  End Sub

  Private Sub HelloPaint_Paint( ByVal sender As Object, _
                                ByVal e As PaintEventArgs) _
                                Handles MyBase.Paint
    e.Graphics.DrawString("Hello Mum!", _
                          New Font("Verdana", 20), _
                          New SolidBrush(Color.Tomato), _
                          40, _
                          40)
  End Sub
End Class

Copy and paste the text into notepad and save the file as HelloPaint.vb. Alternatively, you can download the file HelloPaint.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 helloPaint.vb

Close the command line and run the HelloPaint.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.

Figure 6-1 - Windows Application with paint event

HelloPaint Constructor

Unlike C#, you do not need to attach a PaintEventHandler delegate to the forms Paint event using the += operator. Instead, this is handled for us in the method.

Paint Event Handler

A PaintEventHandler represents the method that will handle the Paint event of a Control. So when the form calls the Paint event to draw the form, our PaintEventHandler will also be executed.

In order for this to work, we must define a method in our class that has the same arguments and return type as the PaintEventHandler in the System.Windows.Forms class named PaintEventArgs.

Private Sub HelloPaint_Paint( ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint
  ' . . .
End Sub 

The code to draw the string is exactly the same as in the previous example and so has been missed out in the code above. However there are two new keywords in the method code above that are worthy of mention. The first is the Object keyword. This is used to define a generic object class, when used in the case above allows the sender to be one of any type derived from object.

The second new keyword is the Handles keyword. This is used to declare that a procedure handles a specified event.


References

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

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

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

For more information on the DrawString method, visit MSDN at microsoft here.

For more information on the Handles keyword, visit MSDN at microsoft here.

What Next?

Next we will look at an empty Visual Studio WinForm application.

Return to the Tutorial Contents.