1 / 68

第 8 章 文件系统管理

第 8 章 文件系统管理. 8.1 通用对话框 8.2 文件处理 8.3 文件系统控件 8.4 File System Object 模型简介. 8.1 通用对话框. 在本节中主要研究 Windows 标准对话框,它为用户提供了功能强大的、专业的交互式对话框。 微软创建的标准对话框为普通用户提供了与所有 Windows 程序相同的界面。. 8.1.1 CommonDialog 控件.

Download Presentation

第 8 章 文件系统管理

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第8章 文件系统管理 8.1 通用对话框 8.2 文件处理 8.3 文件系统控件 8.4 File System Object模型简介 VB程序设计与数据库应用

  2. 8.1 通用对话框 • 在本节中主要研究Windows标准对话框,它为用户提供了功能强大的、专业的交互式对话框。 • 微软创建的标准对话框为普通用户提供了与所有Windows程序相同的界面。 VB程序设计与数据库应用

  3. 8.1.1 CommonDialog控件 1.CommonDialog 控件提供诸如打开和保存文件、设置打印选项、选择颜色和字体等操作的一组标准对话框。运行 Windows 帮助引擎时,控件还能够显示帮助。 2.CommonDialog 控件可以显示如下常用对话框:“打开”、“另存为”、“颜色”、“字体”、“打印”。 VB程序设计与数据库应用

  4. 3. 一个字体对话框 : 8.1.1 CommonDialog控件 VB程序设计与数据库应用

  5. 8.1.2 使用通用对话框 1.添加通用对话框控件 通用对话框必须用CommonDialog控件来建立,若未添加 CommonDialog 控件,则应选定【工程】→【部件】命令,将控件添加到工具箱中 。 VB程序设计与数据库应用

  6. 8.1.2 使用通用对话框 2.CommonDialog控件有如下的方法: ShowOpen方法显示“打开”对话框。 ShowSave方法显示“另存为”对话框。 ShowColor方法显示“颜色”对话框。 ShowFont方法显示“字体”对话框。 ShowPrinter方法显示“打印”对话框。 ShowHelp方法 显示Windows帮助对话框。 VB程序设计与数据库应用

  7. 8.1.2 使用通用对话框 3. 调用这些方法的一般格式:   控件名.方法 例如:CommonDialogl.ShowSave 显示“另存为”对话框 CommonDialog1.ShowOpen 显示“打开”对话框 VB程序设计与数据库应用

  8. 程序运行结果: 8.1.3 通用对话框应用实例 VB程序设计与数据库应用

  9. 1. 创建应用程序 添加控件、菜单 8.1.3 通用对话框应用实例 VB程序设计与数据库应用

  10. 2. 添加通用对话框(CommonDialog)控件 8.1.3 通用对话框应用实例 VB程序设计与数据库应用

  11. 8.1.3 通用对话框应用实例 3. 添加程序代码 • 显示“颜色”对话框的方法: 在mnuColor的Click事件处理中键入下列代码: Private Sub mnuColor_Click() On Error GoTo colorerr CommonDialog1.ShowColor Text1.BackColor = CommonDialog1.Color Exit Sub colorerr: MsgBox "您放弃了选择对话框!" Exit Sub End Sub VB程序设计与数据库应用

  12. 8.1.3 通用对话框应用实例 • 显示“字体”对话框的方法: 在mnuFont的Click事件处理中键入下列代码: Private Sub mnuFont_Click() On Error GoTo fonterr CommonDialog1.Flags = cdlCFEffects Or cdlCFBoth CommonDialog1.ShowFont Text1.FontName = CommonDialog1.FontName Text1.FontSize = CommonDialog1.FontSize Text1.FontBold = CommonDialog1.FontBold Text1.FontItalic = CommonDialog1.FontItalic Text1.FontUnderline = CommonDialog1.FontUnderline Text1.FontStrikethru = CommonDialog1.FontStrikethru Text1.ForeColor = CommonDialog1.Color Exit Sub VB程序设计与数据库应用

  13. 8.1.3 通用对话框应用实例 fonterr: MsgBox "您放弃了选择对话框!" Exit Sub End Sub VB程序设计与数据库应用

  14. 8.1.3 通用对话框应用实例 • 显示“打开”对话框的方法: 在mnuOpen的“Click”事件处理中键入下列代码: Private Sub mnuOpen_Click() On Error GoTo openerr CommonDialog1.Filter = _ "all files(*.*)|*.*|textfiles(*.txt)|*.txt| batch files(*.bat)|*.bat" CommonDialog1.FilterIndex = 2 CommonDialog1.ShowOpen MsgBox "您选择的文件是:" + CommonDialog1.FileName Exit Sub openerr: MsgBox "您放弃了选择对话框!" Exit Sub End Sub VB程序设计与数据库应用

  15. 8.1.3 通用对话框应用实例 • mnuExit的Click事件处理中键入下列代码: Private Sub mnuExit_Click() Unload Form1 End Sub 4.运行应用程序 注:代码见:/第八章/例题/prjexp8_1.prj VB程序设计与数据库应用

  16. 颜色对话框 8.1.3 通用对话框应用实例 VB程序设计与数据库应用

  17. 字体对话框 8.1.3 通用对话框应用实例 VB程序设计与数据库应用

  18. 打开对话框 8.1.3 通用对话框应用实例 VB程序设计与数据库应用

  19. 选中文件名称消息框 8.1.3 通用对话框应用实例 VB程序设计与数据库应用

  20. 8.2 文件处理 • 根据不同的分类标准,文件可分为不同的类型。 VB程序设计与数据库应用

  21. 8.2.1 文件的类型 1.按存取方式分类 可分为顺序文件和随机文件 • 顺序存取只能依先后次序存取文件中的数据。 • 随机文件可以直接存取文件中指定的数据。 2.按性质分类 可分为程序文件和数据文件 3.按存储格式分类 可分为ASCII码文件和二进制文件 VB程序设计与数据库应用

  22. 8.2.2 顺序文件 1.顺序文件的打开和关闭 (1)打开文件 Open pathname For [Input | Output | Append] As filenumber [Len = buffersize] 例如:要建立一个新的顺序文件,名称为DataFilel: Open "DataFilel" For Output As #1 VB程序设计与数据库应用

  23. 8.2.2 顺序文件 (2)关闭文件 Close [[#]filenumber][,[#]filenumber]… 例如:Close#1,#2 Close VB程序设计与数据库应用

  24. 8.2.2 顺序文件 2.顺序文件的读和写 (1)顺序文件的写语句 ① Print #语句  Print #filenumber,expressionlist VB程序设计与数据库应用

  25. 8.2.2 顺序文件 例:当用户单击【写入文件】按钮时,用Print #来将文本框中的文本存到file.txt文件中。 Private Sub Command1_Click() Open "D:\file.txt" For Output As #1 Print #1, Text1.Text Close #1 End Sub 注:代码见:/第八章/例题/prjexp8_2.prj VB程序设计与数据库应用

  26. 8.2.2 顺序文件 ② Write #语句:  Write #filenumber,expressionlist 例:单击【写入文件】按钮,打开一个名为data.dat的文件,并将两个数字写到该文件中,这两个数字是用户已输入到Textl和Text2两个文本框中的。 Private Sub Command1_Click() Open "D:\data.dat" For Output As #1 Write #1, Val(Textl.Text),Val(Text2.Text) Close #1 End Sub 注:代码见:/第八章/例题/prjexp8_3.prj VB程序设计与数据库应用

  27. 8.2.2 顺序文件 (2)顺序文件的读语句 ① Input #语句:  Input #filenumber,expressionlist VB程序设计与数据库应用

  28. 8.2.2 顺序文件 例: 单击【读取文件】按钮,用Input #方法读回文件data.dat中的那些整数,把它们显示在文本框中。 Private Sub Command1_Click() Dim str1, str2 As String Open "D:\data.dat" For Input As #1 Input #1, str1, str2 Text1.Text = str1 Text2.Text = str2 Close #1 End Sub 注:代码见:/第八章/例题/prjexp8_4.prj VB程序设计与数据库应用

  29. 8.2.2 顺序文件 ② Line Input #语句: Line Input #filenumber,string VB程序设计与数据库应用

  30. 8.2.2 顺序文件 • 例:当用户单击【读取文件】按钮时,用Line Input #在文件file.txt中逐行读并在文本框中显示出来。 Private Sub Command1_Click() Dim NewLine As String Open "D:\file.txt" For Input As #1 Do Until EOF(1) Line Input #1, NewLine Text1.Text = Text1.Text + NewLine + vbCrLf Loop End Sub 注:代码见:/第八章/例题/prjexp8_5.prj VB程序设计与数据库应用

  31. 8.2.2 顺序文件 ③ Input$函数: Input$(numberbytes,#filenumber) VB程序设计与数据库应用

  32. 8.2.2 顺序文件 3.顺序文件应用实例 例:建立数据文件,通过输入框输入数据,并将数据存入文件DataFile.txt中,然后在窗体上显示出来 Option Explicit Private Sub Form_Click() '输入数据 Open "D:\DataFile.txt" For Output As #1 Dim j As Integer Dim x As Integer, y As Integer, z As Integer VB程序设计与数据库应用

  33. 8.2.2 顺序文件 For j = 1 To 3 x = InputBox("请输入x的值") y = InputBox("请输入y的值") z = InputBox("请输入z的值") Print #1, x, y, z Next j Close #1 VB程序设计与数据库应用

  34. 8.2.2 顺序文件 '读取数据 Open "D:\DataFile.txt" For Input As #1 For j = 1 To 3 Input #1, x, y, z Print x, y, z Next j Close #1 End Sub 注:代码见:/第八章/例题/prjexp8_6.prj VB程序设计与数据库应用

  35. 8.2.2 顺序文件 运行结果 : VB程序设计与数据库应用

  36. 8.2.2 顺序文件 例:建立数据文件,通过输入框输入关于学生信息的多条数据,包括学号、姓名、性别和成绩。并将数据存入文件DataFile1.txt中。用Input$函数来一次读文件中一个字符,并将它显示在立即窗口。 Private Sub Form_Click() Dim MyChar '输入数据 Open "D:\DataFile1.txt" For Output As #1 Dim j As Integer Dim id As String, name As String, sex As String, score As Single VB程序设计与数据库应用

  37. 8.2.2 顺序文件 For j = 1 To 3 id = InputBox("请输入学号:") name = InputBox("请输入姓名:") sex = InputBox("请输入性别:") score = InputBox("请输入成绩:") Print #1, id, name, sex, score Next j Close #1 VB程序设计与数据库应用

  38. 8.2.2 顺序文件 '读取数据 Open "D:\DataFile1.txt" For Input As #1 Do While Not EOF(1) MyChar = Input$(1, #1) Debug.Print MyChar Loop Close #1 End Sub 注:代码见:/第八章/例题/prjexp8_7.prj VB程序设计与数据库应用

  39. 8.2.2 顺序文件 • 运行结果 : VB程序设计与数据库应用

  40. 8.2.3 随机文件 1.随机文件的打开和关闭 (1)定义记录类型 Type Person ID As Integer Name As String * 20 MonthlySalary As Currency LastReviewDate As Long Title As String * 15 ReviewComments As String * 150 End Type VB程序设计与数据库应用

  41. 8.2.3 随机文件 (2)打开随机访问文件 Open pathname [For Random] As filenumber Len = reclength 例如:打开一个名叫MyFile.txt的文件的语句: Open "D:\MyFile.txt" For Random As #1 Len = 100 VB程序设计与数据库应用

  42. 8.2.3 随机文件 (3)关闭文件 Close [[#]filenumber][,[#]filenumber]… VB程序设计与数据库应用

  43. 8.2.3 随机文件 2.随机文件的读和写 (1)随机存取文件的写语句 Put #filenumber,Position,varname 例:在一个模块中定义一个名为Person的新类型 ,当用户单击【写入文件】按钮时,用Textl和Text2文本框中的文本填充记录。 VB程序设计与数据库应用

  44. 8.2.3 随机文件 Type Person Name As String * 20 ID As String * 10 End Type Dim WriteData As Person Private Sub Command1_Click() WriteData.Name = Text1.Text WriteData.ID = Text2.Text Open "D:\records.dat" For Random As #1 Len = 30 Put #1, , WriteData Close #1 End Sub VB程序设计与数据库应用

  45. 8.2.3 随机文件 VB程序设计与数据库应用

  46. 8.2.3 随机文件 (2)随机存取文件的读语句 Get #filenumber,Position,varname 例:现在来看怎样将上例中的那条记录读回。当用户单击【读取文件】按钮时,将上例中的那条记录读回并显示在Text3和Text4文本框中。 Dim ReadData As Person Private Sub Command2_Click() Open "D:\records.dat" For Random As #1 Len = 30 Get #1, , ReadData Close #1 Text3.Text = ReadData.Name Text4.Text = ReadData.ID End Sub VB程序设计与数据库应用

  47. 8.2.3 随机文件 注:代码见:/第八章/例题/prjexp8_8.prj VB程序设计与数据库应用

  48. 8.2.3 随机文件 3.随机文件应用实例 声明一个记载学生考试信息的自定义变量类型数据,将学生考试信息写入文件DataFile2.txt中。在窗体上建立两个命令按钮,单击第一个按钮,将要求输入学号、姓名、性别、成绩。不输入学号将结束数据录入。单击第二个按钮,将所有数据文件读出,将学号、姓名、性别、成绩在立即窗口中显示出来。 VB程序设计与数据库应用

  49. 8.2.3 随机文件 Option Explicit Private Type Stud '声明自定义变量Stud ID As String * 10 Name As String * 20 Sex As String * 10 Score As Single End Type Dim Student As Stud '输入数据 Private Sub Command1_Click() Open "D:\DataFile2.txt " For Random As #1 Len = 50 VB程序设计与数据库应用

  50. 8.2.3 随机文件 Do While True Student.ID = InputBox("请输入学号:") If Trim(Student.ID) = "" Then Print "结束数据输入" Close #1 Exit Do End If Student.Name = InputBox("请输入姓名:") Student.Sex = InputBox("请输入性别:") Student.Score = InputBox("请输入成绩:") Put #1, , Student Loop End Sub VB程序设计与数据库应用

More Related