In this month's column Helen Bradley shows you how to add a menu to your Visual Basic projects. The menu you'll produce here offers you a choice of backgrounds for your main form and an About screen too. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
How to use the files
on this disc 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.
|
Figure 1 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:
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):
Now add a new form using Insert, Form - this will be Form2 and add this control and set these properties:
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.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() Figure 3: The About form shows information about the program and loads over the top of the main form. |
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 colors 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 color effect which is a cut down version of the code that appeared in the May issue of PC User magazine.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|