vel19.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 1,273 行 · 第 1/3 页
HTM
1,273 行
<BR><P>Adding the mnuFileExit_Click() event procedure requires only that you select that menu command during the program's development. At the Form window, click the File menu bar command. Visual Basic displays the File pull-down menu. Even though you're not running the program but are working on the program from the Form window, the File menu appears to show you what happens when the user selects File at runtime.<BR><P>Click the Exit item on the File pull-down menu. As soon as you click Exit, Visual Basic opens the Code window to a new event procedure named mnuFileExit_Click(), as shown in Figure 19.7.<BR><P><B> <A HREF="19vel07.gif">Figure 19.7. Visual Basic opens the Code window when you click a menu item.</A></B><BR><P>This event procedure is simple. When the user selects File Exit, you want the application to terminate. Therefore, insert the End statement to the body of the mnuFileExit_Click() procedure and close the procedure by double-clicking its control button.<BR><P>Although this unit doesn't take the time to complete the menu bar's pull-down menus or add event procedure code to every menu item, perhaps it would also be good to hook up the File Open menu command to display the file-selection frame that appears when the user selects the Display a File command button on the form. Rather than duplicate the Display a File command button's code inside the mnuFileOpen_Click() procedure, you can execute the command button's Click event by adding the following line to the body of the mnuFileOpen_Click() procedure:<BR><BR><PRE><FONT COLOR="#000080">cmdSel_Click ' Execute the Display a file event</FONT></PRE><P>As you can see, adding event procedures requires little more than clicking the menu item and adding the body of the procedure that appears.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Add yet another event procedure to the View Highlighted menu option by clicking its menu item and adding the code so that the procedure looks like that in Listing 19.1.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>After building your menu, you must tie code to the various menu items by writing Click event procedures that will execute when the user runs the application and selects from the menu. If any menu command duplicates the functionality of other controls, such as command buttons, don't copy the command button's code into the body of the menu event procedure. Instead, simply execute that command button's event procedure from the menu item's event procedure.<BR><P><FONT COLOR="#000080"><B>Listing 19.1. Code that boldfaces the displayed file.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub mnuViewHighlighted_Click ()2: ' Determines if the file being displayed3: ' appears Boldfaced or not4: mnuViewHighlighted.Checked = Not (mnuViewHighlighted.Checked)5: txtFile.FontBold = mnuViewHighlighted.Checked6: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Output: </I></B></FONT>Figure 19.8 shows a textual data file being displayed without a boldface font. Before you added the View Highlighted menu item, the file always appeared in a boldface font.<BR><P><B> <A HREF="19vel08.gif">Figure 19.8. When unchecked, the </B><B>V</B><B>iew </B><B>H</B><B>ighlighted menu item turns off the file </B><B>display's boldface font.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>When the user first runs the program, the View Highlighted item will be checked, meaning that the file that the user displays will appear in the boldfaced font.<BR><P>Listing 19.1 uses the Not operator to set the FontBold property of the text box that displays the file in the application. Line 4 resets the check mark on the View Highlighted menu item. If the item is checked, line 4 sets the value to the opposite value using Not. The check mark will then disappear and won't be displayed if the user displays the View pull-down menu once again. Line 5 sets the FontBold property of the file's text box to the same True or False condition that the Checked item is now set to.<BR><BR><A NAME="E68E146"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B> Add an Extra Touch to Help</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Using the Chr$(), you can add a little-known trick to right justify the Help menu bar item.<BR><P>Some applications display a menu bar similar to the one that you added to MYMENU.MAK, except that those applications right justify the Help menu item to appear at the far right edge of the menu bar, as shown in Figure 19.9.<BR><P><B> <A HREF="19vel09.gif">Figure 19.9. Using a trick, you can right justify the Help menu bar item.</A></B><BR><P>If you add an ASCII 8, the backspace control character, to a menu bar's Caption property, that menu bar item will appear at the right of the menu bar. Why the backspace control character? Nobody really knows!<BR><P>You can't easily add the backspace during the application's design, but you can add the backspace character using Chr$() during the program's start-up code. All you have to do is concatenate the Help menu bar item's caption to the Chr$(8) character. You must concatenate the Chr$(8) at runtime. Perhaps the best location to add the Chr$(8) is in the Form_Load() procedure (the procedure that executes before the user sees the form). Listing 19.2 contains the code needed to right justify the Help menu bar option.<BR><P><FONT COLOR="#000080"><B>Listing 19.2. Code that right justifies the Help menu bar item.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub Form_Load ()2: ' Right-justify the Help menu bar item3: mnuHelp.Caption = Chr$(8) & mnuHelp.Caption4: End Sub</FONT></PRE><P>This unit doesn't do anything with the Help option because adding online help to an application is beyond the scope of this book. You can concatenate any menu bar command to the Chr$(8) if you want that command to appear right justified.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>If you concatenate the Chr$(8) character to more than one menu bar command, each one will be right justified against the right edge of the menu bar.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>By concatenating Chr$(8) at runtime, you can right justify menu bar commands against the right edge of the menu bar. Don't overdo the right justification, however. If you right justify any menu bar command, you should probably right justify only the Help command.<BR><P>One of this book's disks contains the MYMENU.MAK application in its current state under the filename MYMENU2.MAK (as well as the form named MYMENU2.FRM). Therefore, if you had trouble with one of the menus or event procedures described here, you can load and study the MYMENU2.MAK application to see where you went wrong.<BR><P>This unit ends here without adding event procedures to all of the menu items because you follow these same steps to add event procedures to the remaining items. You now understand how to add the menu bar, submenu items, and event procedure code to your applications, and you're well on your way to creating Windows programs that rival those of the pros!<BR><BR><A NAME="E68E147"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E131"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>What Visual Basic tool do you use to design menus?<BR><BR><LI>True or false: The Menu Design window enables you to specify property values for your application's menus.<BR><BR><LI>True or false: You can add Ctrl+keystroke shortcut access keys to your menu bar commands.<BR><BR><LI>True or false: You can add Alt+keystroke shortcut access keys to your menu bar commands.<BR><BR><LI>What is the Name prefix that you should use for all menu items?<BR><BR><LI>What is a <I>submenu</I>?<BR><BR><LI>Which command buttons in the Menu Design window enable you to rearrange menu options?<BR><BR><LI>Which command buttons in the Menu Design window enable you to indent menu items to indicate that those items are part of a pull-down menu?<BR><BR><LI>Which menu property enables you to add check marks next to menu items?<BR><BR><LI>Which menu property enables you to hide menu items from the user at various times?<BR><BR><LI>True or false: Menu commands can be part of a control array.<BR><BR><LI>What Caption property do all separator bars require?<BR><BR><LI>What is the event name that all menu selections trigger?<BR><BR><LI>What character right justifies menu items?<BR><BR><LI>True or false: You can right justify more than one menu bar command.<BR><BR></OL><BR><A NAME="E69E132"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Find the Bug</B></FONT></CENTER></H4><BR><OL><LI>Frank wants to right justify his View menu item. Frank attempts to type this into the menu item's Caption property: Chr$(8) & &View. Explain to Frank how to add the backspace character properly.<BR><BR></OL><BR><A NAME="E69E133"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>What would you name a menu item with the caption Split that appears when the user selects from the Window menu bar?<BR><BR><LI>What name would you give to two separator bars located on the View pull-down menu?<BR><BR><LI>Describe how you could gray out the menu items in MYMENU.MAK that have no event procedure code (such as File New) at this time.<BR><BR></OL><BR><A NAME="E69E134"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><P>Create a blank application with the following menu bar items: A, B, C, D, and E. Write code in the Form_Load() procedure to right justify the E item. On each pull-down menu, add the following items: 1, 2, 3, 4, and 5. Insert separator bars between the 2 and 3 and the 4 and 5. After you complete this, run the program to make sure that your menu items all appear as expected. After you complete this simple Menu Design window exercise, you will have mastered the basics of adding menu commands.<BR><P ALIGN=LEFT><A HREF="velp09.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel20.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?