Sign In
Link to us
Home
1. Introduction
2. Introducing VB
3. Projects
4. Containers
5. Controls
6. Code
7. Forms
 7.1 Menus
 7.2 MDI Forms
 7.3 Modal Forms
 7.4 Form Properties

8. Debugging Code
9. Error Handling
10. Objects
11. User Controls
12. Data Projects
13. API
14. Distribution

Other Resources


Affiliated Links:
www.freefunfings.com


7. Forms

7.1 Menus

A Menu is a drop down palette of options that is generally associated with a Form. The Menu itself does nothing other then generate Events when clicked - in the same way as say, a CommandButton.

Visual Basic has a comprehensive Menu editor. This is accessible (when you have a Form selected) from the top toolbar and looks like this:

Selecting the Menu Editor brings up a window with a list of Menu items. The top level (furthest to the left) is the actual Menu itself. Sub Menus are indented using the arrow buttons. Each Menu item has to have it's own "Caption" which will be the displayed text and a "Name". The "Name" is the code reference to that Menu item, just like a control's "Name" is used in the code.

Exercise:
  • For example, to add a "File" menu to your Form, enter the following information into the Menu Editor:
Caption:    &File
Name:       mnuFile
  • Click the "Next" button and then the indent button (right-arrow). On this Menu enter:
Caption:    &Open
Name:       mnuOpen
  • Click "Next" again and add another Menu item:
Caption:    E&xit
Name:       mnuExit
  • Now click the "OK" button to close the Menu Editor dialogue. You will notice that your Form now has a File menu with "Open" and "Exit" options.

Note:  The use of the ampersand symbol ("&") on Menus and other controls tells Visual Basic that the next letter is the shortcut key for that control. In the example above, the "Open" menu option has the ampersand before the 'O'. This is represented in Windows as an underlined 'O'. If the user holds down the Alt key and presses this shortcut key, it will have the same effect as 'clicking' the control. These shortcut keys can be added to most of the controls in Visual Basic, using the ampersand in their Caption property.

Exercise xii:
  • Create a new project in Visual Basic that displays a large form.
  • Add the following Menus to your form:


Hint: The 3D line used to group menu items is called a 'separator'. A separator can be inserted by typing a dash ("-") as the menu item's "Caption" - note, however, that you still have to give the item a Name (even if it is just "Sep1" or something!)

  • Close the Menu Editor dialogue and select the "Exit" Menu option that you created. The Form's Event Code View window will appear at the Event Method for that Menu option. The name of this Event depends on what you named the Menu.
  • In this Event Method unload the form, as before (since this is the only Form that is running, this will actually end the program):
Private Sub mnuExit_Click()
    Unload Me
End Sub
Run the application and test that the "Exit" menu option now works.

7.2 MDI Forms

An MDI Form or "Multi-Document Interface" Form is one which can contain child forms. This is similar to the way that some applications can have several documents open at once. For example, in MS Word, you can have two letters open as 'sub-windows' inside the main Word window.

Each time you click on a child form in the MDI Form, it's menus automatically appear on the MDI Form, rather than at the top of the form itself. If the child form has no menu, the MDI Form's own menu will remain in place.

MDI Forms are used in applications where several forms being shown simultaneously would significantly increase the user's productivity.

Exercise xiii:
  • Create a new "Standard EXE" project in MicrosoftT Visual Basic.
  • From the "Project" menu add one "MDI Form" and two "Forms"
  • Open each of the Forms and use the Properties Window to change the "MDIChild" property to "True". Notice that the Form's icon changes in the Project Explorer window. This tells Visual Basic that these Forms are to be loaded into the MDI Form as child forms.
  • On the MDI Form, add a single "File" menu in the same way as shown above.
  • On Form1, add "File", "Edit" and "Help" menus as above. Don't add any menus to Form2.
  • Double-click the MDI Form to open the Code View window at the Load Event Method. On load of this form, show both of the child forms as follows:
Private Sub MDIForm_Load()
    Form1.Show
    Form2.Show
End Sub
Run the application and test the menus when different child forms are selected.

Within an MDI Form, multiple instances of a child form can be created, using an Object reference. Each instance of this form can then be treated as a copy of the original, possessing all of the Methods, Events and Properties that the original one has.

Exercise xiv:
  • Continue from the previous exercise, adding three Module-Level variables at the top of the MDI Form's code:
Dim frmOne As Form
Dim frmTwo As Form
Dim frmThree As Form
  • This declares three variables of data type Form. In order to create an instance of an existing form, these variables must be Set to the master form. Insert the following lines into the MDI Form's code:
Private Sub MDIForm_Load()
    Set frmOne = New Form1
    Set frmTwo = New Form1
    Set frmThree = New Form1
    -- etc --
  • Each of the instances of the original form (we used Form1 here) now exists as an object variable. Since the object is an exact replica of the original, it has a "Show" method, so we can load each of the three copies - complete the code as follows:
Private Sub MDIForm_Load()
    Set frmOne = New Form1
    Set frmTwo = New Form1
    Set frmThree = New Form1

    frmOne.Show
    frmTwo.Show
    frmThree.Show

    Form2.Show

End Sub
Note:  Notice that Form2 remains as a singular instance, whilst frmOne, frmTwo and frmThree are copies of the original form, Form1.

Test the application. If you like, add some controls to Form1 and re-test the project.

7.3 Modal Forms

When a Form is loaded (using it's "Show" method) it can be shown modally. This means that it will appear on top of all other Forms in your application and keep the focus until it is unloaded. If the user tries to click on any other form, the system beeps.

Modal forms are used for prompts, such a Message Boxes, where the user has to accept a condition or make a choice before the application will continue any further. To load a Form modally, invoke the Form's "Show" method with a modal parameter of 1:

frmMain.Show 1
MDI Child Forms can not be shown modally. If you need a modal form within an MDI application, use a standard Form (i.e. set the "MDIChild" property to false).

7.4 Form Properties

Form properties, like properties of other objects, are accessed either in the design environment by selecting the appropriate property in the Properties Window or programmatically by using the dot (".") modifier at the end of the form name.

There are too many properties to go through each one in details, so here are a few of the more commonly used ones to familiarise yourself with.

7.4.1 BorderStyle
The BorderStyle of a form sets whether the user can move, resize, minimise and maximise it. Forms that are drawn in the Visual Basic design environment could be resized by the user and if not catered for within the code, simply expose large areas of empty space.

BorderStyleDescription
NoneNo border. Not resizable at all. (Over-ridden if menus exist)
Fixed SingleFixed border. Close button only
SizableResizable border and all three buttons (minimise, maximise & close)
Fixed DialogFixed border. Close button only
Fixed ToolWindowFixed border. Close button only. Reduced title bar. No control box.
Sizable ToolWindowReduced Title bar. No control box.

Exercise xv:
  • Create a new "Standard EXE" project in Visual Basic.
  • Draw a Frame control on the form. Now go into the Form's Resize Event method. Each time the Form is resized, we are going to programmatically resize the Frame control to match. Enter the following within the Resize Event:
Private Sub Form_Resize()
    If Me.Width < 1000 Then Me.Width = 1000     ' Limit the minimum width
    If Me.Height < 1000 Then Me.Height = 1000   ' Limit the minimum height
    Frame1.Left = 100
    Frame1.Top = 100
    Frame1.Width = Me.Width - 400
    Frame1.Height = Me.Height - 800
End Sub
  • The Frame control is first positioned to a fixed Left and Top location (this is like the X and Y co-ordinates), then the width and height properties are set to match the Form's (with a margin of 400 for the width and 800 for the height).
Note:  Notice the use of the Me object to describe the current Form. Had we used "Form1", the result would have been the same.

Try changing to different BorderStyles and re-test.

7.4.2 StartUpPosition
The StartUpPosition of a Form is used to determine the location at which the Form will first appear. This can be a position on the screen or central to the client application or screen. The Form Layout window can be used to fix the StartUpPosition of a form. To open the Form Layout window, select "Form Layout Window" from the "View" menu. Select the form that you want to position in the Project Explorer window, the toggle through the option in the StartUpPosition property of the form.

Note:  Using the Form Layout window drag the form to a new position. Notice that the form's StartUpPosition property is automatically changed to "Manual". This is because you have 'manually' specified where the form should appear.

7.4.3 AutoRedraw
Normally, controls are drawn onto the form during it's loading and are not generally refreshed with graphical changes. The AutoRedraw property tells Visual Basic to continually refresh the form, so that as graphics are added to it, they are shown immediately.

7.4.4 ShowInTaskBar
This Form property is used to determine if the form's title will appear as an entry in the Windows Task Bar (the bar with the Start button on it). You may want to only display the main form's caption, as the others may only be dialogues etc.

The Task Bar is used to switch between operating applications within the Windows environment. It is important to keep the user's productivity in mind when deciding which forms to show in the task bar.

<<Page 6.7  :   Top  :   Page 8>>

©vbtraining.co.uk