Add a menu to your Visual Basic projects with Helen Bradley's tutorial. |
||||||||||||||||||||||||||||||||||||||||||||||||||
The menu you'll
produce here offers you a choice of backgrounds for your main form and an About screen,
too. Figure 1d shows the Background menu open with the choices listed and the currently
selected one checked. To create the project start a new project and add these controls and set these properties: |
||||||||||||||||||||||||||||||||||||||||||||||||||
![]() Figure 1d: This month's project is a menu for your Visual Basic projects to allow the user to alter the background for the main form. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
![]() Figure 2d: The Menu Editor is where the menu is designed and should look like this when you are finished. |
Click on the form and select Tools, Menu Editor and type in the Caption
text box '&Background' and in the Name text box 'mnuBackground'. Make sure Enabled and
Visible check boxes are checked and Checked is cleared, and click Next to continue. The
next menu item has as its Caption 'Picture #&1', its Name is 'mnuPicture', its Index
is 0 and it should be Enabled and Visible but its Checked check box should be cleared.
Press the right pointing arrow once to make this a second level menu item (it will have an
ellipsis in front of it) and select Next to continue. Now, repeat this process to add these menu items (to move between creating first and second level menu items press the left or right pointing arrows): |
|||||||||||||||||||||||||||||||||||||||||||||||||
![]() Figure 3d: The About form shows information about the program and loads over the top of the main form. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
The Menu Editor screen should look like the one in Figure 2d when you are
finished designing the menu. Select OK to finish. Now add a new form using Insert, Form -- this will be Form2 and add this control and set these properties: |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
This form is the About form so add a label or two with some suitable
information about the program, add a graphic (BMP, ICO or WMF) in a picture box and size
the form appropriately. Figure 3d shows the About form displaying over a smaller than full
screen version of the main form. Type the text from Code Box #1 into Form1 and the text from Code Box #2 into Form2. Save the project, call Form1 main.frm, call Form2 about.frm and call the project menu.vbp. Run the program to test the menu. |
||||||||||||||||||||||||||||||||||||||||||||||||||
![]() The files on the CD won't all work in your version of Visual Basic so you need to use the ones that will. Start by opening the VBP file, if you get errors then you can't use it. You will, however, be able to copy the code from the TXT file to save you typing it. Just make sure you have named all your controls correctly so that the code will match them. The files main.frm, about.frm, main.frx,
about.frx and main.vbx are on this months disc as is the text of
the code in main.txt and about.txt. You will find them
in the \interact\diymenu folder on the CD. |
How it works In the Menu editor the Caption is what your user will see on the menu when they select the various options and the Name property is the name you use to refer to the menu items in your VB code. The items on the Background menu have been created as a control array of menu items because they are easier to process this way. Each of the items is mutually exclusive so if you have one turned on all the others must, by definition, be turned off. The control array allows us to easily identify which menu item was chosen as its index number is stored by Visual Basic in the Index variable. The mnuPicture_Click subroutine controls what happens when the user selects a menu item, first it turns off the checkmark against the previously selected menu option (its value is stored in the screenBackground variable). The value of the most recently selected menu item is stored in screenBackground and the checkmark next to this menu item is set. Then a case statement is executed to change the background image to one of the pictures in the invisible picture boxes, the form fade colours from the May issue of the magazine or no background at all (created by loading no picture at all). The Index option on the Help menu has been created but disabled as there is no help index available. Setting the Enabled property of a menu item to false allows you to show the menu item but make it unavailable to the user. If you select the About option on the Help menu the procedure mnuAbout_Click runs and loads Form2 (the about form) as modal so the user must close down the About form before they can continue. The two invisible picture boxes on Form1 simply act as storage space for the graphics which will be used as the form background. The remainder of the code controls the fading colour effect which is a cut-down version of the code that appeared in the May '97 issue.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
|