C#简易记事本
来源:互联网 发布:淘宝卖东西包装怎么办 编辑:程序博客网 时间:2024/05/21 22:52
//简单版记事本,功能尚不完善,bug也有很多,继续完善中...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 记事本{ public partial class Main : Form { public Main() { InitializeComponent(); } private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)//新建文件 { if (textBox1.Text.Length != 0)//判断文本框有无内容 { DialogResult result = MessageBox.Show("是否将更改保存?", "记事本",MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.Cancel)//选择不保存 { textBox1.Text=""; } else//选择保存 { this.saveFileDialog1.Filter = "*.txt|*.txt"; this.saveFileDialog1.ShowDialog(); string file = this.saveFileDialog1.FileName; if (string.IsNullOrEmpty(file)) return; //以下为写字符到文本文件,需要添加System.IO引用 //创建一个文件流 FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write); //创建一个StreamWriter对象 StreamWriter sw = new StreamWriter(fs); sw.Write(this.textBox1.Text); //释放StreamWriter对象,文件流对象 sw.Dispose(); fs.Dispose(); textBox1.Clear(); } } } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)//打开操作 { this.openFileDialog1.Filter = "*.txt|*.txt"; this.openFileDialog1.ShowDialog(); string file = this.openFileDialog1.FileName; if (string.IsNullOrEmpty(file)) return; //以下为写字符到文本文件,需要添加System.IO引用 //创建一个文件流 FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read); //创建一个StreamWriter对象 StreamReader sr = new StreamReader(fs); this.textBox1.Text = sr.ReadToEnd(); //释放StreamWriter对象,文件流对象 sr.Dispose(); fs.Dispose(); } private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)//保存操作 { if (textBox1.Text.Length != 0)//判断文本框内有无内容 { this.saveFileDialog1.Filter = "*.txt|*.txt"; this.saveFileDialog1.ShowDialog(); string file = this.saveFileDialog1.FileName; if (string.IsNullOrEmpty(file)) return; //以下为写字符到文本文件,需要添加System.IO引用 //创建一个文件流 FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write); //创建一个StreamWriter对象 StreamWriter sw = new StreamWriter(fs); sw.Write(this.textBox1.Text); //释放StreamWriter对象,文件流对象 sw.Dispose(); fs.Dispose(); } } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { if (textBox1.Text.Length != 0) { DialogResult result = MessageBox.Show("是否将更改保存?", "记事本",MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.Cancel)//选择不保存 { Application.Exit(); } else//选择保存 { this.openFileDialog1.Filter = "*.txt|*.txt"; this.openFileDialog1.ShowDialog(); string file = this.openFileDialog1.FileName; if (string.IsNullOrEmpty(file)) return; FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read); //创建一个StreamWriter对象 StreamReader sr = new StreamReader(fs); this.textBox1.Text = sr.ReadToEnd(); //释放StreamWriter对象,文件流对象 sr.Dispose(); fs.Dispose(); } } else { Application.Exit(); } } private void textBox1_TextChanged(object sender, EventArgs e)//初始时有一些按钮不能使用 { //当文本框有内容时改变状态 保存ToolStripMenuItem.Enabled = true; 复制ToolStripMenuItem.Enabled = true; 剪切ToolStripMenuItem.Enabled = true; 删除ToolStripMenuItem.Enabled = true; 撤销ToolStripMenuItem.Enabled = true; } private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode==Keys.A&&e.Control)//键盘控制全选 { textBox1.SelectAll(); } if (e.KeyCode==Keys.F5) {//事件注册 textBox1.KeyDown += new KeyEventHandler(时间ToolStripMenuItem_Click); } } private void 全选CtrlAToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.SelectAll(); } private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Paste(); } private void 复制ToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Copy(); } private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Cut(); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Cut(); } private void 查找ToolStripMenuItem_Click(object sender, EventArgs e) { Find f = new Find(); f.Show(); } private void 替换CToolStripMenuItem_Click(object sender, EventArgs e) { Exchange ee = new Exchange(); ee.Show(); } private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.WordWrap = false; } private void 时间ToolStripMenuItem_Click(object sender, EventArgs e) { string words = textBox1.Text; textBox1.Text = DateTime.Now.ToString() + "\r\n"; textBox1.Text += words; } private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) { fontDialog1.ShowColor = true; fontDialog1.ShowApply = true; fontDialog1.ShowDialog(); textBox1.Font = fontDialog1.Font; textBox1.ForeColor = fontDialog1.Color; } private void 关于ToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("制作人:田成琳 时间:2014.11.13"); } private void 查看帮助ToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("功能完善中,目标尽量与记事本功能基本相同。"); } }}
运行结果:
心得体会:查找和替换功能没有实现,复制、剪切、删除、粘贴那块偷懒了(*^__^*) ……
0 0
- 简易记事本---C#窗体
- C#简易记事本
- C#简易记事本
- C#第二次作业:简易记事本
- C#第二次作业-简易记事本
- 简易记事本开发(第二次C#作业)
- 《C#程序设计》第二次作业 简易记事本
- 简易记事本
- 简易记事本
- 简易记事本
- C#第二次作业之目标1:“简易记事本”开发
- 简易记事本java源码
- android 简易记事本
- 简易记事本,V0.1
- Windows Phone 简易记事本
- 一个简易的记事本
- Java - 简易记事本
- java简易记事本
- Android 高仿微信6.0主界面 带你玩转切换图标变色
- Swift教程_swift常见问题(0005)_完美解决Cannot override 'dealloc'异常
- hive安装部署
- C++ cin输入流详解
- 类的成员对象变量何时构造
- C#简易记事本
- java特性 多态
- 为什么很多人不喜欢做程序员?甚至讨厌程序员?
- Qt on Android:资源文件系统qrc与assets
- poj 3208 Apocalypse Someday(数位dp)
- Null value was assigned to a property of primitive type setter of cn.itcast.oa.domain.Forum.topicCou
- poj 1182 食物链
- 使用jsp过滤器实现登录权限
- 添加手势交互