vel17.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 2,041 行 · 第 1/4 页
HTM
2,041 行
<BR><PRE><FONT COLOR="#000080">1: Sub cmdDisp_Click ()2: ' Set default values3: drvDrive.Drive = "c:"4: dirList.Path = "c:\" ' Root directory5: 6: ' Display the file-selection frame7: lblDirs.Caption = dirList.Path8: txtFiles.Text = "*.txt"9: fraFile.Visible = True10: ' Select the first file11: ' in the list of files12: filFile.ListIndex = 013: End Sub</FONT></PRE><P>By setting the Drive and Path properties of the drive list box and the directory list box in lines 3 and 4, the command button sets up a default listing for the C: drive and the root directory on the C: drive. Line 7 initializes the label that shows the selected directory name above the directory box (directly beneath the Directories: label). Line 8 initializes the File Name text box with a default file pattern. A common filename extension for text files is txt, so the initial pattern is set to *.txt.<BR><P>Line 9 does the work of displaying the frame to the user. During the FILESEL.MAK's development, the frame's Visible property was set to False, so the user doesn't see the frame or its controls until the user presses the command button. Finally, line 12 selects the first item in the file list box so that a filename is always selected in the list upon the initial display of the frame. If no files exist for the given drive, directory, and pattern, line 12 has no effect on the program.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Manage the drive change first.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>When writing a file-selection frame, always code the drive change event procedure before anything else. When the user changes the drive, you must ensure that the directory list changes as well. Listing 17.2 contains the Change event procedure for the drive.<BR><P><FONT COLOR="#000080"><B>Listing 17.2. Code that executes when the user selects a different drive.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub drvDrive_Change ()2: dirList.Path = drvDrive.Drive3: End Sub </FONT></PRE><P>The drvDrive_Change() event procedure may not seem to do much. Actually, the purpose of the procedure is to trigger <I>another</I> event. As soon as the user changes the drive, Listing 17.2 works to update the path of the directory to the new drive's directory (the current default directory that's selected on the newly chosen drive). Therefore, when the user selects a new drive, Listing 17.2 acts to update the directory list, <I>which </I><I>in turn</I> triggers the directory's Change event procedure shown in Listing 17.3.<BR><P><FONT COLOR="#000080"><B>Listing 17.3. When the directory changes, so must the file list.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub dirList_Change ()2: ' The selection of the directory3: ' list box changed4: filFile.Path = dirList.Path5: ' Make sure that one file is selected6: If (filFile.ListCount > 0) Then7: ' Select the first file8: ' in the list of files9: filFile.ListIndex = 010: End If11: 12: lblDirs.Caption = dirList.Path13: End Sub</FONT></PRE><P>Line 4 updates the file list box to reflect the path of the new directory chosen. Lines 5 through 10 ensures that a file is selected, even if the user didn't select one. Line 9 assigns the selection to the first file in the file list. Line 12 updates the pathname caption that appears beneath the Directories label showing the current directory. The chain reaction is now tied together so that a change in the drive changes both the directory and the file list. If the user changes only the directory and not the drive, the change in the directory (due to the dirList_Change() event procedure) changes also.<BR><P>There is one Change event that's not tied to this chain-reaction of drive-directory-file change, but one that's important to maintain in all your applications. If the user wants to see a different set of files in the current directory, the user can enter a new file pattern in the File Name text box. As soon as the user enters a new pattern, the Change event procedure shown in Listing 17.4 executes.<BR><P><FONT COLOR="#000080"><B>Listing 17.4. The user just changed the filename pattern.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub txtFiles_Change ()2: filFile.Pattern = txtFiles.Text3: End Sub</FONT></PRE><P>Any change in the File Name text box produces an immediate corresponding change in the list of files shown. Therefore, as soon as the user changes the default pattern from *.txt to *.exe, for example, the filename list box updates to show only those files whose extensions are .exe.<BR><P>The user is allowed to close the file-selection dialog using any of the following three methods:<BR><UL><LI>Click the OK command button<BR><BR><LI>Double-click a filename<BR><BR><LI>Click the Cancel command button<BR><BR></UL><P>Listing 17.5 contains the Click event procedure for the OK command button. When the user closes the file-selection frame, several things must take place. The highlighted filename needs to be saved. Line 3 saves the highlighted filename by placing that filename in the File Name text box. Even though the frame will be hidden (as well as all controls on the frame, including the text box) by line 5, the text box will be available to the program, and any routine in the application can access txtFiles.Text to see what filename the user selected. In addition, the drive and directory controls will still hold their selected values, even though the user won't be able to see these controls.<BR><P><FONT COLOR="#000080"><B>Listing 17.5. The OK closing routine.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub cmdFileOK_Click ()2: ' Save the file selected by the user3: txtFiles.Text = filFile.List(filFile.ListIndex)4: ' Hide the file selection frame5: fraFile.Visible = False6: ' Tell surrounding code that Cancel was not pressed7: DidCancel = False ' User didn't press Cancel8: ' Show the "You Selected" command button9: cmdSel.Visible = True10: End Sub</FONT></PRE><P>There is a module variable named DidCancel, defined in the form module's (general) procedure, that holds either True or False, depending on whether the user clicks Cancel. If Listing 17.5 executes, the user selected OK, not Cancel, so line 7 sets the DidCancel variable to False in case any application using this frame needs to know whether Cancel was pressed. Line 9 closes out the event procedure by hiding the file selection frame and all its controls.<BR><P>If the user double-clicks a filename, the one-line DblClick event procedure shown in Listing 17.6 executes. The DblClick event procedure does nothing more than call the OK command button's Click event procedure because the same result occurs: the file selected by the user becomes the requested file and the frame disappears from the user's view.<BR><P><FONT COLOR="#000080"><B>Listing 17.6. The user selected a file by double-clicking the filename.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub filFile_DblClick ()2: ' Double-clicking a selected filename3: ' does the same thing as selecting a4: ' file and pressing the OK button.5: ' Therefore, call the OK button's Click event.6: Call cmdFileOK_Click7: End Sub</FONT></PRE><P>If the user clicks the Cancel command button, nothing should change in the program except that the frame disappears and the DidCancel variable should be set to True. Therefore, if you add this frame and its event procedures to your own applications, the DidCancel variable tells you whether the user pressed Cancel. Listing 17.7 shows the Cancel command button's Click procedure.<BR><P><FONT COLOR="#000080"><B>Listing 17.7. The user clicked Cancel to close the file-selection frame.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub cmdCancel_Click ()2: ' Used pressed the Cancel button3: ' Hide the frame and inform the program4: DidCancel = True5: fraFile.Visible = False6: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The code behind the file selection frame describes how the file controls work in conjunction with each other. A user's change to one of the file controls can affect the other file controls. Use code to control those additional changes so that a drive or directory change updates a corresponding file list box.<BR><P>Please remember that if you adopt this file-selection frame for your own application, the disk drive appears at the start of the pathname. Therefore, you don't have to read the drive list box as long as you use the full path supplied by the path list box's Path property.<BR><BR><A NAME="E68E131"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E119"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>Which is considered more long-term: Data in variables or data in files?<BR><BR><LI>Why is using a dialog to get a filename from the user safer than using an input box to request the filename?<BR><BR><LI>What is a dialog box?<BR><BR><LI>How do you implement dialog boxes using the Visual Basic Primer system?<BR><BR><LI>How do you mimic the display and disappearance of dialog boxes when you use frames?<BR><BR><LI>How many controls on the toolbox work with files?<BR><BR><LI>When can you set a default drive list box: at design time or runtime?<BR><BR><LI>What does the Pattern file list control property contain?<BR><BR><LI>Why must you maintain synchronization with a frame's file controls?<BR><BR><LI>When a frame contains the file controls, which file-related control should you manage first when writing code to keep the controls in sync?<BR><BR><LI>Which file controls should change when the user clicks the drive list box?<BR><BR><LI>Which file controls should change when the user clicks the directory list box?<BR><BR><LI>How does a calling application determine whether the user pressed the FILESEL.MAK file frame's Cancel button?<BR><BR><LI>Which event procedure keeps a file list current when the user changes the selected pattern of files?<BR><BR><LI>True or false: When you hide a frame, all controls on that frame disappear also.<BR><BR></OL><BR><A NAME="E69E120"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>Write a file list pattern than displays all files whose filenames begin with ACCT.<BR><BR><LI>Write a file list pattern that displays all files whose filenames end with the extension ex1, ex2, ex3, and so on.<BR><BR><LI>Write the code that sets a drive list named drvDisk to point to all files on the D: drive and that sets a directory list named direng to all files in the VBPRIMER directory.<BR><BR></OL><BR><A NAME="E69E121"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><P>Change the FILESEL.MAK to enable the user to select from files that already exist or enter a new file name. You'll have to add to the frame's closing code so that the program checks the text box entry against the selected file list.<BR><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="#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="vel18.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 + -
显示快捷键?