您的位置:首页 > 编程语言

简单的记事本功能实现代码--(流读取器)

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));

}
}
}


使用流读取器注意命名空间的引用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: