|
|
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
|
7. Forms7.1 MenusA 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.
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:
Caption: &File Name: mnuFile
Caption: &Open Name: mnuOpen
Caption: E&xit Name: mnuExit
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:
![]()
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!)
Private Sub mnuExit_Click()
Unload Me
End Sub
Run the application and test that the "Exit" menu option now works.
7.2 MDI FormsAn 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:
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:
Dim frmOne As Form Dim frmTwo As Form Dim frmThree As Form
Private Sub MDIForm_Load()
Set frmOne = New Form1
Set frmTwo = New Form1
Set frmThree = New Form1
-- etc --
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 FormsWhen 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 1MDI 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 PropertiesForm 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
Exercise xv:
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
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 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>> | ||||||||||||||||