velp09.htm

来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 599 行

HTM
599
字号
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons velp09.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel19.htm"><LINK REL="Previous" HREF="vel18.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons velp09.htm</H2><P ALIGN=LEFT><A HREF="vel18.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="vel19.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="#E68E140" >Stop &amp; Type</A><UL><LI><A HREF="#E69E127" >The Program's Description</A><LI><A HREF="#E69E128" >Studying the File's Input</A><LI><A HREF="#E69E129" >Descriptions</A><LI><A HREF="#E69E130" >Close the Application</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E27"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Project 9</B></FONT></CENTER></H1><BR><A NAME="E67E30"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Extending Data and Programs</B></FONT></CENTER></H2><BR><BR><A NAME="E68E140"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Stop &amp; Type</B></FONT></CENTER></H3><BR><P>This lesson taught you how to create a file-selection frame that mimics a file dialog box as well as controls the file controls from the toolbox. You learned that your code must keep the file controls in synchronization with each other or problems will occur, such as the directory list box that points to a drive that doesn't contain the directory listed in the directory list box.<BR><P>You learned how to use Visual Basic's file I/O commands and functions to read, append to, and write data files. Accessing data files is relatively simple.<BR><P>In this lesson, you saw the following: <BR><UL><LI>Why you need a file dialog box for file selection<BR><BR><LI>How to manipulate the file controls to keep them in sync<BR><BR><LI>How to open files and associate files to a file number<BR><BR><LI>Why the Write# command is the perfect command for sending data to a data file in an easy-to-read format<BR><BR><LI>When to use Input# and when to use Line Input# to read data from files<BR><BR></UL><BR><A NAME="E69E127"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Program's Description</B></FONT></CENTER></H4><BR><P>Figure P9.1 shows the PROJECT9.MAK opening Form window. As you can see, the project starts off extremely simply by displaying three command buttons.<BR><P><B> <A HREF="P9vel01.gif">Figure P9.1. Project 9's application begins with a simple form.</A></B><BR><P>Two controls, a file-selection frame and a large text box, reside on the form, but these controls are invisible to the user when the user first runs the program. When the user selects the Select a File command button, the file-selection frame that you learned about in <A HREF="vel17.htm">Unit 17</A> appears. The user is expected to select a filename.<BR><P>Once the user selects a filename and double-clicks the name, or presses the OK command button on the file-selection frame, the file-selection frame goes away again (its Visible property is set to False) and the simple three-button form returns. If the user then clicks the Display File command button, the program reads the contents of the selected file into a single (but long) string variable. The program then displays that string variable in the text box whose Visible property is changed to True so that the user can see and scroll through the file. A sample file being displayed in the text box is shown in Figure P9.2.<BR><P><B> <A HREF="P9vel02.gif">Figure P9.2. Displaying the contents of the selected file.</A></B><BR><BR><A NAME="E69E128"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Studying the File's Input</B></FONT></CENTER></H4><BR><P>The majority of PROJECT9.MAK contains the same frame control and code found in <A HREF="vel17.htm">Unit 17</A>'s file-selection frame control. The primary difference lies in PROJECT9.MAK's Display File command button's Click code shown in P9.1.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning: </B>If you display a file that's not an ASCII text file, the file will appear garbaged in the text box. For example, if you select a Microsoft Excel spreadsheet, you won't see the spreadsheet inside the text box, but you will see a compressed binary representation of the spreadsheet.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The code uses the Line Input# statement to read each record in the file that the user selected in the file-selection frame. The line-by-line description explains the code inside the cmdDisp_Click() procedure shown in Listing P9.1.<BR><P><FONT COLOR="#000080"><B>Listing P9.1. Reading an entire file into a string variable.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub cmdDisp_Click ()2: ' Read the whole file (up to 30,000 characters)3: ' into a single string variable. Display that4: ' string variable in a text box.5: Dim count As Integer ' Holds character count6: Dim FileSpec, ALine As String7: Dim FileHold As String ' Holds entire file8: Dim NL As String9: 10: NL = Chr$(13) + Chr$(10)11: 12: ' Gather the filename into a single string13: ' Add an ending backslash if the path has none14: If (Right$(dirList.Path, 1) &lt;&gt; &quot;\&quot;) Then15: FileSpec = dirList.Path &amp; &quot;\&quot; &amp; txtFiles.Text16: Else17: FileSpec = dirList.Path &amp; txtFiles.Text18: End If19: 20: ' Open the file21: Open FileSpec For Input As #122: 23: ' Read up to the first 30,000 characters24: Line Input #1, ALine ' Read a line25: Do Until (EOF(1) = True)26: ALine = ALine + NL ' Add a newline27: ' Make sure that the read won't overshoot string limit28: If (count + Len(ALine)) &gt; 30000 Then29: Exit Do30: End If31: 32: FileHold = FileHold &amp; ALine33: count = Len(FileHold) ' Update count34: Line Input #1, ALine ' Read a line35: Loop36: Close #137: 38: txtFile.Text = RTrim$(FileHold)39: txtFile.Visible = True40: End Sub</FONT></PRE><BR><A NAME="E69E129"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Descriptions</B></FONT></CENTER></H4><BR><P>1: The command button to display the file is named cmdDisp, hence the event procedure subroutine name of cmdDisp_Click().<BR><P>2: A remark helps explain the purpose of the procedure.<BR><P>3: The remark continues.<BR><P>4: The remark continues.<BR><P>5: Define an integer variable that will hold the length of the string variable as the program reads the file into the variable.<BR><P>6: Define a string named FileSpec that will hold the pathname and filename. Also define a string named ALine that will hold each record from the file.<BR><P>7: Define a string variable that will hold the entire file.<BR><P>8: Define a string variable that will hold the carriage return and line feed or newline character.<BR><P>9: A blank line helps separate parts of the procedure.<BR><P>10: Define the newline character by concatenating a carriage return and line feed character together.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>10: Use the ASCII table when you need to concatenate control codes.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>11: Blank lines help separate parts of code.<BR><P>12: A remark that explains this section of the procedure.<BR><P>13: The remark continues.<BR><P>14: If the selected path doesn't end with a backslash, the code must add one.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>14: A backslash, \, always precedes a filename.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>15: Concatenate a backslash after the selected path and before the filename.<BR><P>16: Otherwise... (the selected path already ends with a backslash).<BR><P>17: Concatenate the selected path and filename.<BR><P>18: Terminate the If.<BR><P>19: Blank lines help separate parts of code.<BR><P>20: A remark explains the purpose of this section of code.<BR><P>21: Open the selected file for input. Assign the file to the filenumber 1.<BR><P>22: Blank lines help separate parts of code.<BR><P>23: A remark helps explain this section of the code.<BR><P>24: Read the first record.<BR><P>25: Loop as long as the end of file is not yet reached.<BR><P>26: Append a newline character to the record just read.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>26: Line Input# does not read the file's newline character combination.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>27: A remark explains this section of the code.<BR><P>28: String variables can contain only slightly more than 30,000 characters. Therefore, ignore the record just read and quit reading if the record length puts the number of characters over 30,000 characters.<BR><P>29: Terminate the procedure if the string limit was reached.<BR><P>30: Terminate the If.<BR><P>31: A blank line helps separate parts of the code.<BR><P>32: Append the record to the string variable that holds all the records read to that point.<BR><P>33: Update the count of the file length read so far.<BR><P>34: Read the next line from the file.<BR><P>35: Continue the loop.<BR><P>36: Close all files when you're done with them.<BR><P>37: A blank line helps separate parts of the code.<BR><P>38: Trim off any excess spaces from the file string and display the file in the text box.<BR><P>39: Make the text box visible.<BR><P>40: Terminate the subroutine procedure.<BR><BR><A NAME="E69E130"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Close the Application</B></FONT></CENTER></H4><BR><P>You can now exit the application and exit Visual Basic. The next lesson explains how to add menus to your application and manipulate a new control, the timer control.<P ALIGN=LEFT><A HREF="vel18.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="vel19.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 + -
显示快捷键?