Visual Basic.NET编程PPT电子课件教案-第16章 文件访问和管理.ppt_第1页
Visual Basic.NET编程PPT电子课件教案-第16章 文件访问和管理.ppt_第2页
Visual Basic.NET编程PPT电子课件教案-第16章 文件访问和管理.ppt_第3页
Visual Basic.NET编程PPT电子课件教案-第16章 文件访问和管理.ppt_第4页
Visual Basic.NET编程PPT电子课件教案-第16章 文件访问和管理.ppt_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第16章 文件访问和管理 16.1文件基本类和异常 16.2 文件打开/保存控件 16.3 文件打印/预览控件 16.4 编制“记事本”程序 16.1 文件基本类和异常 n.net文件类:表16-1 n.net文件异常:表16-2 n.net文件访问机制:文件流 n文件流:一系列字节,也叫字节流。 n字节流类:filestream n文件种类:文本文件、二进制文件 n文本文件字节流类: n读文件时使用streamreader n写文件时使用streamwriter n二进制文件字节流类: n读文件时使用binarystreamreader n写文件时使用binarystreamwriter n读文件步骤: n确定文件是否存在 n打开文件 n创建读流:streamreader、binaryreader n使用reader方法读文件 n关闭文件 16.1 文件基本类和异常 n写文件步骤: n打开文件 n如果文件已经存在,应确定是覆盖文件还是在原 来文件后添加新数据 n创建写流:streamwriter、binarywriter n使用write方法写文件 n关闭文件 16.1 文件基本类和异常 n名称空间: imports system.io nfile类:用于打开文件,成员见表16-3。 n打开文件方法:使用open、opentext方法 n关闭文件方法:使用close方法 nfilestream类:用于得到被打开文件的字节流。 nfilemode枚举:用于指定打开文件的模式。枚举值 见表16-4。 nfileaccess枚举:用于指定文件操作模式。枚举值 有read、write、readwrite。 16.1 文件基本类和异常 n.net文件访问操作方法灵活、多样:本章只介绍最 简单的方法。 n代码示范1: dim fs as filestream fs=file.opentext(“a.txt”) fs.close() 16.1 文件基本类和异常 n代码示范2: dim fs as filestream try fs=file.opentext(“a.txt”) fs.close() catch ex as filenotfoundexception msgbox(“文件不存在。”) end try 16.1 文件基本类和异常 n代码示范3: dim fs as filestream try fs=file.open(“a.txt”, filemode.open) catch ex as filenotfoundexception fs=file.open(“a.txt”, filemode.create,_ fileaccess.readwrite) end try fs.close() 16.1 文件基本类和异常 nstreamreader类:用于读取文件。 n示范代码:假设有文本框textbox1,将文件a.txt打 开并读入到textbox1中。 dim fs as streamreader try fs=file.opentext(“a.txt”) textbox1.text=fs.readtoend fs.close() catch ex as filenotfoundexception msgbox(ex.message) end try 16.1 文件基本类和异常 nstreamreader类:用于读取文件。 n示范代码:可以用streamreader类的构造函数来 打开文件 try dim fs as new streamreader (“a.txt”) textbox1.text=fs.readtoend fs.close() catch ex as ioexception msgbox(ex.message) end try 16.1 文件基本类和异常 nstreamwriter类:用于写出文件。 n示范代码:假设有文本框textbox1,将textbox1 中内容写到文件a.txt中。 dim fs as streamwriter try fs=file.opentext(“a.txt”, filemode.create) fs.write(textbox1.text) fs.close() catch ex as ioexception msgbox(ex.message) end try 16.1 文件基本类和异常 nstreamwriter类:用于写出文件。 n示范代码:可以用streamwriter类的构造函数来打 开文件 try dim fs as new streamwriter (“a.txt”) fs.write(textbox1.text) fs.close() catch ex as ioexception msgbox(ex.message) end try 16.1 文件基本类和异常 16.2 文件打开/保存控件 nopenfiledialog控件:图16-4,用于打开文件。 nsavefiledialog控件:图16-5 ,用于保存文件。 n重要属性: ninitialdirectory:对话框打开时的初始路径 nfilter:对话框文件类型下拉列表中的选项 nfilename:对话框文件名栏目中的文件名 n打开方法:showdialog n返回值: ndialogresult .ok ndialogresult .cancel nopenfiledialog示范代码: dim fd as new openfiledialog() fd.filter=“all files|*.*|text files|*.txt” fd.initialdirectory=“c:” if fd.showdialog=dialogresult . ok then dim fs as new streamreader (fd.filename) textbox1.text=fs.readtoend fs.close() end if 16.2 文件打开/保存控件 nsavefiledialog示范代码: dim fd as new savefiledialog() fd.filter=“all files|*.*|text files|*.txt” fd.initialdirectory=“c:” if fd.showdialog=dialogresult . ok then dim fs as new streamwriter (fd.filename) fs.write(textbox1.text) fs.close() end if 16.2 文件打开/保存控件 16.3 文件打印/预览控件 nprintdialog控件:用于打印文件。 nprintpreviewdialog控件:用于预览打印文件。 npagesetupdialog控件:用于设置打印页面。 n打开方法:showdialog n返回值: ndialogresult .ok ndialogresult .cancel n属性document:指定要打印的文件,是 printdocument类。 nprintdocument类: n定义对象方法1:使用下列语句 imports system.drawing.printing dim printdocument1 as new printdocument n定义对象方法2:从工具箱添加(推荐使用) nprintpage事件:真正的打印/预览过程,代码必须 自编,主要使用graphics(第十七章)。 n说明:如果用方法2,可以在代码窗口建立 printpage的处理过程,如同建立一个控件的click 事件处理过程一样。如果使用方法1,必须将事件处 理过程的名字、参数、handles子句全部手工输入。 16.3 文件打印/预览控件 npageprint示范代码: dim p as new pen(system.drawing.color.red, 3) dim f as new font(“宋体“, 16, fontstyle.bold) dim b as new solidbrush(system.drawing.color.blue) dim bm as new bitmap(“a.jpg“) e.graphics.drawimage(bm, 10, 20) e.graphics.drawline(p, 10, 20, 190, 200) e.graphics.drawstring(“hello“, f, b, 10, 20) e.graphics.fillrectangle(b, 60, 60, 90, 100) n说明:把打印机当成可以绘图的白纸,使用绘图工具将要打印 的内容画上去,这样可以使用任何打印机打印任何要打印的内 容。 n注意:使用e.graphics 16.3 文件打印/预览控件 n打印示范代码: dim pd as new printdialog() pd.document=printdocument1 指定打印文件 if pd.showdialog=dialogresult . ok then printdocument1.print() 调用pageprint过程。 end if 16.3 文件打印/预览控件 n预览示范代码: dim pd as new printpreviewdialog() pd.document=printdocument1 指定打印文件 if pd.showdialog=dialogresult . ok then printdocument1.print() 调用pageprint过程。 end if 16.3 文件打印/预览控件 n页面设置示范代码: dim pd as new pagesetupdialog() pd.document=printdocument1 指定打印文件 if pd.showdialog=dialogresult . ok then printdocument1.defaultpagesettings = pd.pagesettings修改打印设置。 end if 16.3 文件打印/预览控件 n新建项目,使用 imports systems.io n设置form1.text=”记事本” n增加主菜单: - 文件编辑查找格式 其它 - 新建 复制查找滚动条 时间/日期 打开 剪切查找下一个自动换行 关于 保存 删除字体 打印 粘贴颜色 打印预览 全选背景色 页面设置 退出 - n设置菜单项“滚动条” 属性:checked=true n设置菜单项“自动换行” 属性:checked=true 16.4 编制“记事本”程序 n增加文本框textbox1,设置属性 n允许多行:multilines=true n使之与窗体四周对齐:dock=fill n“新建”菜单项“click”事件代码: textbox1.text=” n“打开”菜单项“click”事件代码: 见本章openfiledialog示范代码 n“保存”菜单项“click”事件代码: 见本章savefiledialog示范代码 n“退出”菜单项“click”事件代码: me.close() 16.4 编制“记事本”程序 n增加printdocument控件printdocument1:在代 码窗口增加其“pageprint”事件处理过程,代码如下 : dim f as new font(“宋体“, 16, fontstyle.bold) dim b as new solidbrush(system.drawing.color.blue) e.graphics.drawstring(textbox1.text, f, b, 10, 10) 说明:此段代码只能打印一页。打印多页代码见教材p263-264 。 n“打印”菜单项“click”事件代码: 见16.3打印示范代码 n“预览”菜单项“click”事件代码: 见16.3预览示范代码 n“页面设置”菜单项“click”事件代码: 见16.3页面设置示范代码 16.4 编制“记事本”程序 n“复制”菜单项“click”事件代码: textbox1.copy() n“剪切”菜单项“click”事件代码: textbox1.cut() n“粘贴”菜单项“click”事件代码: textbox1.paste() n“删除”菜单项“click”事件代码: textbox1.selectedtext=” n“全选”菜单项“click”事件代码: textbox1.selectall() 16.4 编制“记事本”程序 n“查找”菜单项所需全局变量: dim skey as string dim start as long n“查找”菜单项“click”事件代码: skey=inputbox(”查找”,”关键字输入框” ) if skey=0 then textbox1.select(start,skey.length) else msgbox(”未发现。”) end if end if 16.4 编制“记事本”程序 n“查找下一个”菜单项“click”事件代码: if skey” then if start=0 then textbox1.select(start,skey.length) else msgbox(”未发现。”) end if end if 16.4 编制“记事本”程序 n“滚动条”菜单项“click”事件代码: if textbox1.scrollbars=both then textbox1. scrollbars =none 滚动条.checked=false else textbox1 .scrollbars=both 滚动条.checked=true end if 说明: both和none要写成 system.windows.forms.scrollbars.both system.windows.forms.scrollbars.none 16.4 编制“记事本”程序 n“自动换行”菜单项“click”事件代码: if textbox1.wordwrap=true then textbox1.wordwrap=false 自动换行.checked=false else textbox1.wordwrap=true 自动换行.checked=true end if 16.4 编制“记事本”程序 n“字体”菜单项“click”事件代码: dim fd as new fontdialog() if fd.showdialog()=dialogresult.ok then textbox1.font=fd.font end if n“颜色”菜单项“click”事件代码: dim cd as new colordi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论