📄 vel04.htm
字号:
<HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>If you now saved AUTOLOAD.MAK to the disk, all future projects that you create would look just like the AUTOLOAD.MAK that you now see. Before you save AUTOLOAD.MAK, however, read a little further to learn about another file that you should add to the Project window.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Add the CONSTANT.TXT file to lighten the rest of your programming burden.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>named constant</I> is a constant value with a name that is easy to remember.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>There will be several times when you have to set various controls to specific values from a list of possibilities. For example, a label control can have a boxed border around it if you set the value of a certain property to 1—a <I>fixed single-line border</I>—and no border if you set it to 0. Instead of setting properties with those hard-coded values, you can use descriptive named constants such as NONE and FIXED_SINGLE.<BR><P>The designers of Visual Basic took every possible control property value and assigned names to them so that you can use either the names, which are usually easier to remember than actual numbers, or the values themselves. Once you get used to using named constants, you will rarely use the actual values.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning: </B>Don't fret. The use of named constants will make a lot more sense when you begin building your own applications.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>All the named constants are stored in a file named CONSTANT.TXT. You can add CONSTANT.TXT to your AUTOLOAD.MAK's Project file so that all the applications you eventually create automatically contain that file as well. Follow these steps to add CONSTANT.TXT to the Project window of AUTOLOAD.MAK:<BR><OL><LI>Select File Add File from the menu bar. Visual Basic displays a File Open dialog box.<BR>Visual Basic assumes that you want to add either a form (.FRM) file, a Visual Basic language (.BAS) file, or a custom control description (.VBK) file. Therefore, Visual Basic displays only those kinds of files. Instead of adding one of those files, you want to add a text file, which typically has a .TXT filename extension, as in CONSTANT.TXT. Therefore, you must override the suggested filenames in the File Name prompt.<BR><BR><LI>Type CONSTANT.TXT for the filename, and press Enter or click the OK command button. Immediately, Visual Basic adds CONSTANT.TXT to the project window, as shown in Figure 4.3.<BR><BR><LI>Save the AUTOLOAD.MAK file so that the changes you have made using File Save Project are reflected in all future projects that you create.<BR><BR></OL><P><a href="04vel03.gif"><B>Figure 4.3. The CONSTANT.TXT file is part of the Project window.</B></a><BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The AUTOLOAD.MAK file determines what all subsequent projects that you create will initially look like. If you remove the custom control files that you find in AUTOLOAD.MAK's Project window and add the CONSTANT.TXT file, all new projects that you create will initially contain CONSTANT.TXT.<BR><BR><A NAME="E68E37"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Quick to the Draw!</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Finally! You will now use Visual Basic to create your very own fully working Windows program. If you don't think that you know enough about Visual Basic to write programs, hang on because you will see how easy creating a program can be. Follow these steps to create your first Visual Basic application:<BR><OL><LI>Select File New Project to open a new application with a Project window that looks like AUTOLOAD.MAK's that you just saved. Visual Basic opens a new project and a blank Form window on which you will place controls.<BR><BR><LI>Double-click the label control. Remember that the label control is the uppercase <I>A</I> on the Toolbox window. A blank label control (with the <I>terrible</I> default Name property of Label1) appears in the center of the Form window.<BR><BR><LI>Press F4 to bring the Properties window into view.<BR><BR><LI>Scroll the Properties window to the Caption property. The Caption property, by default, contains the name of the label. Because the Caption property holds the label's text, you should change the text. If you highlighted the Caption property, type <B>My First!</B> and press Enter. As Figure 4.4 shows, the label immediately displays the new caption.<BR><BR><P><B> <A HREF="04vel04.gif">Figure 4.4. After changing the label's </B><B>caption.</A></B><BR><LI>Click and hold the mouse cursor over the label that you just added, and drag the label towards the top of the Form window. Leave about an inch between the top edge of the label and the top edge of the window. Center the label under the title of the form. By default, the title is the name of the form, Form1. (You will change control names from their default names in the next unit.)<BR><BR><LI>Double-click the command button control on the Toolbox window. A command button appears in the center of the Form window.<BR><BR><LI>Type the following exactly as you see it: <B>E&xit</B>. The ampersand (&) causes the x to be underlined, as you can see when you look at the resulting command button's caption. The Caption property is the first property that changes because the <I>last</I> property that you changed when you worked with the label control was its Caption property.<BR><BR><LI>Center the command button by dragging the command button a little to the left. Once you center the command button, double-click it. Immediately, Visual Basic opens a Code window as shown in Figure 4.5.<BR><BR><P><B> <A HREF="04vel05.gif">Figure 4.5. The Code window opens when you </B><B>double-click a control.</A></B><BR><LI>Visual Basic knows that you want to write an event procedure that executes whenever the user clicks the command button. You can tell that Visual Basic opened an event procedure for a command button keypress because the name of the procedure is Command1_Click (). (Don't worry about the parentheses right now.) An event procedure always takes the following format:<BR>ControlName_EventName ()<BR>The default name for a new command button (until you change the Name property) is Command1, and the event that triggers when the user clicks a command button is Click. Therefore, the code that handles this command button's click is Command1_Click ().<BR><BR><LI>The two lines that Visual Basic adds to the event procedure are called <I>wrapper lines</I> or <I>wrapper code</I> because they wrap around the code you add to the event procedure. For this event, simply press Tab and add <B>End</B>. The full event procedure should look like this:<BR>Sub Command1_Click ()<BR> End<BR>End Sub<BR><BR><LI>That's it. To see your handiwork, close the Code window and press F5 to run the application you just created. The application's form appears on the screen with its two controls, as shown in Figure 4.6.<BR>To terminate your application and return to Visual Basic, click the Exit command button. As soon as you click the command button, the event procedure that you added executes. The End statement that you added to the event procedure also executes. The sole purpose of End is to terminate the application.<BR><BR></OL><P><a href="04vel06.gif"><B>Figure 4.6. Your first application works like a charm!</B></a><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>Other command button events: Other events are possible with command buttons. Open the Code window and look at the other event names. Click the down arrow at the right of the Code window's Proc: dropdown list. Scroll through the list. You will see that there is a DragDrop event for when the user drags the command button with the mouse and a KeyDown button for when the user presses the command button's shortcut access key, among others.<BR>Other kinds of controls, such as the list box or label control, might have events that are identical to those of the command button control, but the other controls also have different events. The Code window's Object: dropdown list always contains a list of the application's current objects—such as the form and any controls you have added—and the Proc: dropdown combo list contains a list of events for each of the controls that you highlight in the Object: list.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>If you want, you can save this application. However, the disk that comes with this book contains the full application; it is called MYFIRST.MAK. Therefore, if you save your work, save it under a different filename.<BR><P>When you save your applications, Visual Basic wants you to name both the form and the entire project. Usually, especially for the one-form applications that you will write in this book, you name the form the same name as the project, but both have different filename extensions. Suppose that you want to save the project under the name FIRST. To save the project, follow these steps:<BR><OL><LI>Select File Save Project. Visual Basic opens the Save As dialog box for the form. Type <B>FIRST</B> and press Enter. Visual Basic adds the .FRM extension.<BR><BR><LI>Visual Basic now displays the Save Project As dialog box. Type <B>FIRST</B> and press Enter to save the project. Visual Basic adds the .MAK extension.<BR><BR><LI>You can now exit Visual Basic and take a deserved rest.<BR><BR></OL><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>With a few keystrokes and mouse clicks, you can create a fully working Visual Basic application. If you rerun the MYFIRST.MAK application, you will see that you can maximize, minimize, and resize the application's window. Visual Basic automatically adds a control button in the upper-left hand corner of the window with which you can control the application from the Windows system level.<BR><BR><A NAME="E68E38"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E32"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>What is an event?<BR><BR><LI>Why are GUI-based programming environments such as Windows event-driven?<BR><BR><LI>True or false: Windows passes all events that happen in your program to Visual Basic.<BR><BR><LI>What are properties?<BR><BR><LI>What must you write to handle events?<BR><BR><LI>Name two properties for any control that you can think of after reading this unit.<BR><BR><LI>True or false: Forms have properties.<BR><BR><LI>True or false: Forms are objects.<BR><BR><LI>Which window do you use for changing property values?<BR><BR><LI>What is program maintenance?<BR><BR><LI>What advantage does using a three-letter prefix offer?<BR><BR><LI>Why should you write Windows programs that look and work in a manner that is consistent with other Windows programs?<BR><BR><LI>What is the name of the file that describes all new projects?<BR><BR><LI>What is the name of the file that holds named constants?<BR><BR><LI>What are named constants?<BR><BR><LI>True or false: Visual Basic assigns well-named control names by default.<BR><BR><LI>How can you open a Code window for a control's primary event property?<BR><BR><LI>How are event procedures named?<BR><BR><LI>What are the first and last statements in an event procedure called?<BR><BR><LI>What Visual Basic command terminates a running program?<BR><BR><LI>What does the Code window's Object: dropdown combo list contain?<BR><BR><LI>How does the filename of the form differ from the name of the project?<BR><BR><LI>What object does frmStartUp describe?<BR><BR><LI>What object does cboNameChoice96 describe?<BR><BR><LI>What object does cmdPrintIt describe?<BR><BR></OL><BR><A NAME="E69E33"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Find the Bug</B></FONT></CENTER></H4><BR><OL><LI>Victor the Visual Basic programmer just got a new CD-ROM with tons of great fonts. Victor decides to use a different font for every word on his form. Can you help show Victor the light?<BR><BR><LI>Describe what is wrong with each of these Name properties:<BR><BR><OL TYPE=A><LI>End<BR><BR><LI>96JanSalesList<BR><BR><LI>cmdStar$<BR><BR><LI>July-List<BR><BR></OL></OL><BR><A NAME="E69E34"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><P>Create a new project that contains two control buttons. Add a Beep statement to the wrapper of the first button’s Click event procedure, and change the Caption property to Ring a Bell. (Beep is a command that rings the PC's bell.) Change the second command button's Caption property to Exit, and type End for the Click event procedure. Run the program and test your results.<BR><P ALIGN=LEFT><A HREF="vel03.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="velp02.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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -