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


In the last section we added a label to display "Hello Mum!" on the form. This time we shall draw directly on the form itself.

Below is the complete listing of the new program. In order to be able to download each file I have changed the class name from HelloLabel to HelloPaint.

Listing 5-1 - Windows Application with OnPaint

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

  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    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 5-1 - Windows Application with OnPaint

A Note About Splitting Lines In VB

This took me a little while to understand. Unlike in C#, where you can split a program statement over multiple lines and the program will compile. In VB you cannot split program statements unless you use the marker shown above in the DrawString() method. The special marker is ( _) including a space before the underscore. This allows you to split a program statement for ease of reading your code.

The OnPaint Method

In order to draw on the form, every form has an OnPaint method. This allows derived classes to handle the paint event (see references below).

Our method Overrides the base class method allowing us to draw the text we want. This is done using the Graphics class of the System.Drawing namespace. We use the DrawString method, passing it the string we wish to draw, a font to draw it in, a colour and the top-left coordinates of where to put it.

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  e.Graphics().DrawString("Hello Mum!", _
                        New Font("Verdana", 20), _
                        New SolidBrush(Color.Tomato), _
                        40, _
                        40)
End Sub 

The ByVal keyword indicates that an argument is passed in such a way that the called procedure or property cannot change the value of a variable underlying the argument in the calling code.

Setting the Size of the Application

One new feature we have added to the form, is the ClientSize attribute of the Form class. We set it using a new Size structure. Here we have set the Width of the Form to 300 and the Height of the Form to 200.


References

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

For more information on the paint event, visit MSDN at microsoft here.

For more information on the System.Drawing namespace, visit MSDN at microsoft here.

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

For more information on the Argument Passing ByVal, visit MSDN at microsoft here.

For more information on the ClientSize property, visit MSDN at microsoft here.

For more information on the Size structure, visit MSDN at microsoft here.

What Next?

Next we will create the same program, but we will draw the text directly on to the windows form with a paint event.

Return to the Tutorial Contents.