Forms and message boxes
Difficulty:   Lessons: 1, 2, 3, 4

by Lorenzo Dalla Vecchia

The positioning of forms

In the Learn Visual Basic tutorial I had told you that a form could be considered a special kind of control. As all other controls, each form can be positioned on the screen using two properties:

Both numbers are entered in twips, a distance unit used in VB, equivalent to 1/20 of a printer dot. You can either set Left and Top at design time from the Properties box, or you can set them at runtime, for example in the Form_Load procedure:

    Form1.Left = 2000   'distance from left edge of screen
    Form1.Top = 3400    'distance from top edge of screen

If you prefer to type the distances in pixels, you can use the following syntax:

    Form1.Left = 150 * Screen.TwipsPerPixelX
    Form1.Top = 300 * Screen.TwipsPerPixelY

The above code will position the form 150 pixels far from left edge and 300 pixels far from left edge. The TwipsPerPixelX and TwipsPerPixelY properties of the Screen object return the number of twips needed to cover a horizontal and vertical screen pixel respectively.

You can prevent the user from moving a form around the screen by setting the Moveable property of the form to False.

Another way to position a form is choosing its startup position. You can do this at design time through the StartUpPosition property. The available options are:

Last thing, we'll see how to put a form in front of or behind other forms. This is done through the ZOrder method. Here's the syntax:

   formname.ZOrder z-index

The z-index number defines the position of the form relatively to other forms. For example, 0 will put the form on top, 1 just behind, 2 behind 1 and so on.
Example:

    Form5.ZOrder 1    '2nd level
    Form9.ZOrder 0    '1st level
    Form7.ZOrder 2    '3rd level

With the above code, Form9 will be visible on top, Form5 just behind Form9 and Form7 behind all others.
The last important thing: however you position forms on the Z axis, you can select and bring a form to top by using the SetFocus method:

    Form7.SetFocus

This will bring Form7 on top even if we ZOrder-ed it to 2.

The look of a form

Now let's talk about a form's look.
The most important property in deciding the appearance, but also the behaviour, of a form is BorderStyle. The following table summarizes the various types of forms and their corresponding BorderStyle value.

Border-less No caption.
No border.
Can't be resized.
BorderStyle = 0
Fixed-size Caption with icon.
Thin border.
Can't be resized.
BorderStyle = 1
Resizable Caption with icon.
Thick border.
Can be resized.
BorderStyle = 2
Dialog Box Caption w/o icon.
Thin border.
Can't be resized.
BorderStyle = 3
Fixed-size Toolbox Small caption w/o icon.
Thin border.
Can't be resized.
BorderStyle = 4
Resizable Toolbox Small caption w/o icon.
Thick border.
Can be resized.
BorderStyle = 5

The title bar

The title bar of a form is the blue bar on the top containing the caption (or title). It is important because it is also used to drag forms around the screen. The title bar is automatically attached to all forms, except for the border-less form type. You can change the text in the title bar by using the Caption property (either at runtime or design-time). Example:

    Form1.Caption = "This is the title of the form"

The minimization button is used to put the form on the taskbar. To show/hide it, set the MinButton property of the form to True or False.
The maximization button resizes the form to full screen. It can be enabled/disabled by setting the value of the MaxButton property of the form to
True or False.
The control box on the right contains the control menu of the form (it was used a lot in Windows 3.x), but also shows the icon of the form. You can remove it by setting the ControlBox property of the form to
False, but all other buttons (except the close button) will also disappear.

If you want to customize the icon of the form (the one shown on the right and on the taskbar) you must use the Icon property of the form. The Icon property is a special type of property that has not a value, but stores a picture file. You can set it at design time: in the properties window, select the Icon property, click on the "..." button and choose an ICO file to use as icon. If you want to set or change the icon at runtime, use the following code:

    Set formname.Icon = LoadPicture("iconpath")

The above means: "load the picture in iconpath and store it in the Icon property of formname". Example:

    Set Form1.Icon = LoadPicture("C:\Pictures\MyIcon.Ico")

Another thing you can do to improve the look of a form is putting a background image on it: just use the Picture property of the form the same way you operate with Icon.

Lesson 1

Your feedback/requests about tutorials?
Let me know! Click here.

Lesson 3

Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: www.inwind.it