简单的记事本功能实现代码--(流读取器)
2016-03-02 17:16
746 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO;//导入命名空间--流 namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Cut(); } private void 复制CToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Copy(); } private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Paste(); } private void 全选AToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.SelectAll(); } private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Undo(); } private string FileName; private void 打开OToolStripMenuItem_Click(object sender, EventArgs e) { //创建打开文件对话框:帮我们去硬盘上查找文件路径 // OpenFileDialog openfiledialog1 = new OpenFileDialog();//创建对象 openFileDialog1.Filter = "文本文件|*.txt|所有文件|*.*";//过滤 DialogResult isok = openFileDialog1.ShowDialog();//显示对话框 if (isok == DialogResult.OK)//通过对话框的返回值来判断是否点的打开按钮 { //获取文件路径 string filename = openFileDialog1.FileName; FileName = filename; //根据文件路径去读取文件:使用流来读取 //FileStream fs = new FileStream(filename,FileMode.Open); //StreamReader sr = new StreamReader(fs,Encoding.Default);//流读取器--Encoding.Default 选择默认编码 StreamReader sr = new StreamReader(filename,Encoding.Default); string text = sr.ReadToEnd();//读取 sr.Close(); // fs.Close(); textBox1.Text = text; } } private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) { if (FileName != "") { FileStream fs = new FileStream(FileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.Default);//流写入 sw.Write(textBox1.Text); sw.Close(); fs.Close(); } else { SaveFileDialog savefiledialog1 = new SaveFileDialog(); savefiledialog1.Filter = "文本文件|*.txt|所有文件|*.*"; DialogResult isok = savefiledialog1.ShowDialog(); if (isok == DialogResult.OK) { string filename = savefiledialog1.FileName; FileStream fs = new FileStream(filename, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.Default);//流写入 sw.Write(textBox1.Text); sw.Close(); fs.Close(); } } } private void 退出XToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void 查找ToolStripMenuItem_Click(object sender, EventArgs e) { Form2 f = new Form2(textBox1.SelectedText,this); f.Owner = this; f.Show(); } private void 替换ToolStripMenuItem_Click(object sender, EventArgs e) { } private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e) { printPreviewDialog1.ShowDialog(); } private void 打印PToolStripMenuItem_Click(object sender, EventArgs e) { printDialog1.Document = printDocument1;//指定打印对话框所对应的打印对象 DialogResult isok = printDialog1.ShowDialog(); if (isok == DialogResult.OK) { printDocument1.Print();//打印文档 } } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //设置打印内容 Font f = new System.Drawing.Font("宋体",6); e.Graphics.DrawString(textBox1.Text,f,System.Drawing.Brushes.Black,new PointF(0,0)); } } }
使用流读取器注意命名空间的引用
相关文章推荐
- 老李分享:Python开发性能测试脚本
- javaWeb 实现文件下载的功能
- 第1章 软件工程基本原理和C++类
- jre、jdk版本的选择
- 如何在一台windows的服务器里安装配置PHP+MYSQL环境
- QTextCodec中的setCodecForTr等终于消失了 (Qt5)
- Qt与MSVC中文乱码问题的解决方案
- ASP.NET MVC ActionResult的其它返回值
- Qt5.0+msvc2010:解決中文乱码的问题
- Java中SimpleDateFormat类parse解析yyyy-MM-dd与yyyy-M-d是否可以混用
- 分辨率验证工具 - 【Window Resizer】的使用 - Google扩展工具
- Python-----sorted
- java5中的 Map 与线程安全
- C++调用lib或dll文件
- Qt lupdate 和 lrealease 命令行使用方式(多语言)
- Python-----filter
- ASP.Net MVC开发基础学习笔记(2):HtmlHelper与扩展方法
- C# 索引器
- Java并发编程:深入剖析ThreadLocal
- C# 退出应用程序办法