vel17.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 2,041 行 · 第 1/4 页
HTM
2,041 行
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel17.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel18.htm"><LINK REL="Previous" HREF="velp08.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel17.htm</H2><P ALIGN=LEFT><A HREF="velp08.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel18.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E127" >What You'll Learn</A><LI><A HREF="#E68E128" >Needed: File Dialogs</A><LI><A HREF="#E68E129" >The File Controls</A><LI><A HREF="#E68E130" >How FILESEL.MAK Manages the File Selection</A><LI><A HREF="#E68E131" >Homework</A><UL><LI><A HREF="#E69E119" >General Knowledge</A><LI><A HREF="#E69E120" >Write Code That...</A><LI><A HREF="#E69E121" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E25"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 9, Unit 17</B></FONT></CENTER></H1><BR><A NAME="E67E28"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>File Controls</B></FONT></CENTER></H2><BR><BR><A NAME="E68E127"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>Needed: file dialogs<BR><BR><LI>The file controls<BR><BR><LI>How FILESEL.MAK manages the file selection<BR><BR></UL><P>This lesson describes how to access files inside Visual Basic applications. The data stored in files is more permanent than data stored in variables. The values of variables are too volatile. If you want to store a history of customer information, for example, you must store that information on the disk. If you kept such information in arrays of variables, you would never be able to exit Visual Basic or turn off the computer because you would lose all the information!<BR><P>Data inside variables is needed for processing, calculating, printing, and storing, but not for long-term storage. Before you can access data stored in files, you must be able to locate data stored on the disk. This unit teaches you how to build common file-management dialog boxes that enable the user to select files stored on the disk. Depending on your application, you may need the user to select a filename, and this unit's file dialog box discussion provides the tools that you'll need to display lists of files from which the user can choose. After the user selects or enters a filename, you'll need to utilize the file I/O commands and functions discussed in the next unit to access the data in the files.<BR><BR><A NAME="E68E128"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Needed: File Dialogs</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>There are several ways to obtain filename information from the user. The most common way is to build a dialog box that enables the user to select a file, directory, and drive from lists that you provide.<BR><P>Figure 17.1 shows a common File Add dialog box that you'll see if you select File Add from Visual Basic. You've seen this and similar File Open dialog boxes in more than one Windows application. The great thing about file dialog boxes is that the user can select from a list of filenames, directories, and drives. Although you could ask the user for a filename using an input box, allowing the user to select from a list means that the user can often find files quickly and more accurately.<BR><P><B> <A HREF="17vel01.gif">Figure 17.1. A dialog box that enables the user to select the filename, directory, and </B><B>drive.</A></B><BR><P>Dialog boxes are nothing more than secondary forms that you add to your application's primary Form window. The list boxes, command buttons, and text boxes that you see on such dialog boxes are nothing more than controls that you place on those secondary forms, just as you do on the primary Form window. We're going to have a small problem with the Visual Basic Primer System, however, that comes with this book: The Visual Basic Primer doesn't allow for more than one form per application. It would seem at first glance that we won't be able to add file-selecting dialog boxes to our applications.<BR><P>Have no fear. The frame control produces frames on which you can place file-selecting controls such as list boxes, command buttons, and text boxes. The frame control won't offer <I>exactly</I> the same kind of usability as a true dialog box because the user can't resize or move the frame. However, the frame does provide basically the same end result that allows us display file-selecting controls when needed.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Technically, you can write a frame control with properties that enable the user to move the frame, but most of the time, the user shouldn't have that much power over controls. The user could easily hide other controls and mess up the application's design.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Figure 17.2 shows the file selection frame that you'll learn how to create and use in this unit. As you can see, the frame offers the same functionality as the dialog box shown in Figure 17.1. After the user selects a file, you can program the frame to disappear from view just as file-selection dialog boxes disappear.<BR><P><B> <A HREF="17vel02.gif">Figure 17.2. A file-selection frame that mimics a file-selection dialog box.</A></B><BR><P>Actually, the frame file-selection dialog box is slightly easier to use than the standard file-selection dialog box because the frame combines the File Type box with the File Name text box. When the user enters a file pattern, such as *.frm in the File Name text box, the frame immediately updates the filename list box to reflect only files that match that pattern.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Load and run the project named FILESEL.MAK that comes on one of this book's companion disks (the disk that does not contain the Visual Basic Primer system). You'll see the application shown in Figure 17.3. Select different drives and directories and notice how the file list updates to reflect the change. The rest of this unit describes the important components of this application.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Although you can't add additional forms to your Visual Basic Primer applications, you can simulate the additional forms by using the frame control to hold sets of controls that mimic dialog boxes. This unit devotes itself to explaining the FILESEL.MAK project so that you'll know how to build such file-selecting frames for your user, and you'll know how to access the selected information after the user finishes selecting a file.<BR><P><B> <A HREF="17vel03.gif">Figure 17.3. This book's FILESEL.MAK project with the file-selecting frame on the </B><B>screen.</A></B><BR><P>The next section, "The File Controls," explains how to use the file controls. Each of the file controls produces a different kind of file-access list box. Figure 17.3 includes callouts to the three list boxes produced by the file controls that you'll learn about in the next section.<BR><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>The FILESEL.MAK application demonstrates how you might implement a file-selection frame. The application does nothing more than display the file-selection frame when the user clicks the Display File command button, and when the user selects a file, the application erases the frame and displays a new command button labeled You Selected... so that the user can see the file, path, and drive of the file selected from the frame earlier.<BR><P>This particular application allows the user to select files but not enter new filenames. In other words, this application forms the basis for a frame that enables the user to open files that already exist. If you wanted to give the user the ability to enter a name not in the list, you would have to modify the application to look in the File Name text box for a new and valid file name.<BR><P>The FILESEL.MAK program uses the frame's Visible property, setting the property to True for displaying the frame and to False when the frame must not appear on the screen.<BR><BR><A NAME="E68E129"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The File Controls</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Visual Basic includes three file controls that you manage from a file-selection frame.<BR><P>Figure 17.4 shows the Toolbox window showing the location of the three file controls. As you know, selecting a file consists not only of choosing a filename but also choosing a directory and a drive. When you place these file controls on a frame, remember that you must draw each control by dragging with the mouse (explained more fully in Lesson 6's second unit, "Checks, Options, and Control Arrays") so that each control stays with the frame when code moves, hides, or displays the frame.<BR><P><B> <A HREF="17vel04.gif">Figure 17.4. The file controls on the Toolbox window.</A></B><BR><P>Using the file-related controls requires that you tie them together through code. In other words, when the user changes the drive name, both the path name and the file list must change also to show the paths and files on the newly selected drive.<BR><P>Table 17.1 lists the properties that appear in the Properties window for the drive list box control. The properties are similar to the ones that you've learned for other controls. The drive list box control contains a list of disk drives that are available on the system running the program. As with any list box control, the user selects from the drive list box at runtime, so you can't set a default drive in the list until runtime.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 17.1. The drive list box properties.</B></FONT></CENTER><BR><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Property</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>BackColor</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Specifies the drive list box's background color, chosen as a hexadecimal color code or from the color palette.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>DragIcon</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Contains the icon that appears when the user drags the drive list box around on the form. (You'll only rarely allow the user to move a drive list box, so the Drag... property settings aren't usually relevant.)</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>DragMode</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds either 1 for manual mouse dragging requirements (the user can press and hold the mouse button while dragging the control) or 0 (the default) for automatic mouse dragging, meaning that the user can't drag the drive list box but that you, through code, can initiate the dragging if needed.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Enabled</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Determines whether the drive list box can respond to events. If set to True (the default), the drive list box can respond to events. Otherwise, Visual Basic halts event processing for that particular control.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontBold</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds True (the default) if the drive are to display in boldfaced characters; False otherwise.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontItalic</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds True (the default) if the drive names are to display in italicized characters; False otherwise.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontName</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Contains the name of the drive list box drive name's styles. Typically, you'll use the name of a Windows TrueType font.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontSize</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds the size, in points, of the font used for the drive names.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontStrikethru</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds True (the default) if the drive names are to display in strikethru letters (characters with dashes through them); False otherwise.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontUnderline</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds True (the default) if the drive names are to display in underlined letters; False otherwise.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>ForeColor</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Specifies the color of the letters in the drive names.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Height</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Contains the height, in twips, of the drive list box.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>HelpContextID</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?