




免费预览已结束,剩余10页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1.1 记事本开发目标1. 文件操作功能。新建文件,打开文件,保存文件,页面设置,打印和退出。2. 文本编辑功能。文本的撤销,文本重复,剪切,复制,粘贴,删除,查找,替换,转到,全选和时间/日期。3. 文本格式编辑。文本自动换行,字体选择,颜色选择,文本居中,左对齐,右对齐,加粗,倾斜和下划线。4. 实现与windows 记事本兼容使用。即用本记事本可以正常打开windows记事本。5. 进度条根据文本框改变而变化、显示系统时间。1.2 记事本界面预览 1.3 记事本功能结构1.4 流程图记事本流程图如下所示:1.5 程序运行环境 记事本开发平台:Microsoft visual studio 2005。记事本开发语言:C#。运行平台:windows 2000 windows XP windows Vista 等。2 记事本窗体设计与功能分析2.1 主窗体(Form1)设计2.1.1 使用组件介绍编辑框richTextBox1,菜单menuStrip1,工具条toolStrip1,右击contextMenuStrip1,系统计时器timer1,进度条toolStripProgressBar1,显示条toolStripStatusLabel1.2.2.2 本记事本的突出特点本记事本的特点:(1)实现对文本内容格式的编辑,如左对齐、右对齐、居中;对字体的颜色、字体、加粗、倾斜和下划线操作。(2)实现正确地与windows记事本兼容使用,无乱码出现。(3)实现对文本的打印与页面设置。2.2 文件操作设计2.2.1 新建菜单新建、按钮新建以及利用组件ContextMenuStrip1,完成右击菜单新建。代码如下:private void 新建NToolStripButton_Click(object sender, EventArgs e) this.richTextBox1.Clear(); 2.2.2 打开与保存【打开】为了实现与windows记事本兼容打开,利用RichTextBoxStreamType 枚举的PlainText成员,作用是用空格代替对象链接与嵌入 (OLE) 对象的纯文本流。代码实现如下:private void 打开OToolStripButton_Click(object sender, EventArgs e) OpenFileDialog fileone = new OpenFileDialog(); fileone.Filter = 文本文件(*.txt)|*.txt|RTF文件(*.rtf)|*.rtf|所有文件(*.*)|*.*; fileone.FilterIndex = 1; if (fileone.ShowDialog() = DialogResult.OK) try filename = fileone.FileName;/用于保存时显示需要保存的文件名 this.richTextBox1.LoadFile(fileone.FileName,RichTextBoxStreamType.PlainText); catch (Exception) MessageBox.Show(Oh,您的打开不成功!); 【保存】为了让C#记事本的文本保存后,用windows记事本打开,在richTextBox的SaveFile中使用利用RichTextBoxStreamType 枚举的TextTextOleObjs成员,即具有 OLE 对象的文本表示形式的纯文本流。代码如下: string filename;private void 保存SToolStripButton_Click(object sender, EventArgs e) SaveFileDialog fileone = new SaveFileDialog(); fileone.Filter = 文本文件(*.txt)|*.txt|RTF文件(*.rtf)|*.rtf|所有文件(*.*)|*.*; fileone.FilterIndex = 1; fileone.FileName = filename;/保存时自动显示需要保存的文件名 if (fileone.ShowDialog() = DialogResult.OK) try this.richTextBox1.SaveFile(fileone.FileName, RichTextBoxStreamType.TextTextOleObjs); catch (ArgumentException) MessageBox.Show(Oh,您的保存不成功!); 2.2.3 打印与页面设计的代码详细于软件包中。2.2.4 窗体按钮“退出”与菜单“退出”【窗体按钮“退出”】打开主窗体的加载事件Load ,首先判断文本框是否为空,为空则直接关闭窗体,否则先保存再退出。代码如下: private void Form1_FormClosing(object sender, FormClosingEventArgs e) /判断文本是否为空,为空则不处理 if (this.richTextBox1.Text = string.Empty) return; /判断文本是否为空,不为空则弹出提示框 else /创建一个退出提示对话框,并获得用户的选择。 DialogResult dr = MessageBox.Show(您的文本内容已改变,退出程序文本内容将丢失,是否要保存?, 退出提示, MessageBoxButtons.YesNoCancel); /如果用户的选择为yes,那么先保存后关闭窗体。 if (dr = DialogResult.Yes) 保存SToolStripMenuItem_Click(sender, e); e.Cancel = false; if (dr = DialogResult.No) e.Cancel = false; if (dr = DialogResult.Cancel) e.Cancel = true; 【菜单“退出”】利用Close()方法,让执行Close()方法后关联到窗体按钮“退出”。private void 退出XToolStripMenuItem_Click(object sender, EventArgs e) /关闭程序 Close(); 2.3 文件编辑设计2.3.1 撤销与重做2.3.1.1 撤销private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e) this.richTextBox1.Undo();2.3.1.2 重做private void 重做toolStripButton6_Click(object sender, EventArgs e) this.richTextBox1.Redo(); 2.3.2 剪切、复制、粘贴和删除2.3.2.1 剪切、复制与粘贴的功能分别可以用richTextBox1的方法Cut()、Copy()、Paste()实现。2.3.2.2 删除private void 删除LToolStripMenuItem_Click(object sender, EventArgs e) this.richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.SelectionStart, richTextBox1.SelectionLength); 2.3.3 全选与时间/日期设置全选代码如下:/设置全选 private void 全选AToolStripMenuItem1_Click(object sender, EventArgs e) this.richTextBox1.SelectAll(); 设置时间/日期如下:/设置时间/日期private void 时间日期F5ToolStripMenuItem_Click(object sender, EventArgs e) this.richTextBox1.Text = richTextBox1.Text.Insert(richTextBox1.Text.Length, System.DateTime.Now.ToString(); 2.3.4 查找、替换和转到在主窗体的主程序代码设计中,添加如下单击事件的代码:/设置RichTextBox的属性,用于查找,转到和替换 public RichTextBox RichTextBox1 get return richTextBox1; set richTextBox1 = value; / 查找处理 private void 查找ToolStripMenuItem_Click(object sender, EventArgs e) formFind findform = new formFind(); findform.Show(this); /转到处理 private void 转到ToolStripMenuItem_Click(object sender, EventArgs e) formGoto gotoform = new formGoto(); gotoform.Show(this); /替换处理 private void 替换ToolStripMenuItem_Click(object sender, EventArgs e) formReplace replaceform = new formReplace(); replaceform.Show(this); 2.3.5 添加查找窗体formFind ,窗体设计如下:代码如下:public partial class formFind : Form / 构造函数 public formFind() InitializeComponent(); radioButton2.Checked = true; /将向下查找设置为默认的查找方式 / 要查找的关键字 private string keyword = string.Empty; int pos = 0; / 向下查找时关键字位置 string text = string.Empty; / 主窗体中字符串变量 int upos = 0; / 向上查找时关键字位置 bool isDown = true; / 是否向下查找 bool isCase = false; / 是否区分大小写 /查找下一个处理 private void button1_Click(object sender, EventArgs e) /关键字为空时直接返回 if (textBox1.Text = string.Empty) return; else /要查找的关键字 keyword = textBox1.Text; /区分大小写时 if (isCase) /向下查找时 if (isDown) pos = text.IndexOf(keyword, pos); /向上查找时 else upos = text.LastIndexOf(keyword, upos); /不区分大小写时 else keyword = keyword.ToLower(); text = text.ToLower(); /向下查找时 if (isDown) pos = text.IndexOf(keyword, pos); /向上查找时 else upos = text.LastIndexOf(keyword, upos); /向下查找时 if (isDown) if (pos = -1) MessageBox.Show(Sorry! 找不到该字符串!); pos = 0; else (Form1)this.Owner).RichTextBox1.Select(pos, keyword.Length); pos+; this.Owner.Activate(); /向上查找时 else if (upos = -1) MessageBox.Show(Sorry! 找不到该字符串!); upos = text.Length; else (Form1)this.Owner).RichTextBox1.Select(upos, keyword.Length); if (upos != 0) upos-; this.Owner.Activate(); / 取消按钮事件 private void button2_Click(object sender, EventArgs e) Close(); / 选择向上查找事件 private void radioButton1_CheckedChanged(object sender, EventArgs e) isDown = !radioButton2.Checked; / 选择向下查找事件 private void radioButton2_CheckedChanged(object sender, EventArgs e) isDown = radioButton2.Checked; / 选择区分大小写时 private void checkBox1_CheckedChanged(object sender, EventArgs e) isCase = checkBox1.Checked; 设置查找窗体加载事件: / 窗体加载事件 private void formFind_Load(object sender, EventArgs e) text = (Form1)this.Owner).RichTextBox1.Text; upos = text.Length; 2.3.6 添加替换窗体formReplace ,窗体设计如下:代码如下:/ 查找窗体 public partial class formReplace : Form / 构造函数 public formReplace() InitializeComponent(); radioButton2.Checked = true; /将向下查找设置为默认的查找方式 private string keyword = string.Empty; / 要查找的关键字 int pos = 0; / 向下查找时关键字位置 string text = string.Empty; / 主窗体中字符串变量 int upos = 0; / 向上查找时关键字位置 bool isDown = true; / 是否向下查找 bool isCase = false; / 是否区分大小写 /查找下一个处理 private void button1_Click(object sender, EventArgs e) /关键字为空时直接返回 if (textBox1.Text = string.Empty) return; else keyword = textBox1.Text; /要查找的关键字 /区分大小写时 if (isCase) /向下查找时 if (isDown) pos = text.IndexOf(keyword, pos); /向上查找时 else upos = text.LastIndexOf(keyword, upos); /不区分大小写时 else keyword = keyword.ToLower(); text = text.ToLower(); /向下查找时 if (isDown) pos = text.IndexOf(keyword, pos); /向上查找时 else upos = text.LastIndexOf(keyword, upos); /向下查找时 if (isDown) if (pos = -1) MessageBox.Show(Sorry! 找不到该字符串!); pos = 0; else (Form1)this.Owner).RichTextBox1.Select(pos, keyword.Length); pos+; this.Owner.Activate(); /向上查找时 else if (upos = -1) MessageBox.Show(Sorry! 找不到该字符串!); upos = text.Length; else (Form1)this.Owner).RichTextBox1.Select(upos, keyword.Length); if (upos != 0) upos-; this.Owner.Activate(); /替换处理 private void button2_Click(object sender, EventArgs e) string newword = textBox2.Text; /关键字为空时直接返回 if (textBox1.Text = string.Empty) return; else /要查找的关键字 keyword = textBox1.Text; /区分大小写时 if (isCase) /向下查找时 if (isDown) pos = text.IndexOf(keyword, pos); /向上查找时 else upos = text.LastIndexOf(keyword, upos); /不区分大小写时 else keyword = keyword.ToLower(); text = text.ToLower(); /向下查找时 if (isDown) pos = text.IndexOf(keyword, pos); /向上查找时 else upos = text.LastIndexOf(keyword, upos); /向下查找时 if (isDown) if (pos = -1) MessageBox.Show(Sorry! 找不到该字符串!); pos = 0; else (Form1)this.Owner).RichTextBox1.Text = (Form1)this.Owner).RichTextBox1.Text.Remove(pos, keyword.Length); (Form1)this.Owner).RichTextBox1.Text = (Form1)this.Owner).RichTextBox1.Text.Insert(pos, newword); (Form1)this.Owner).RichTextBox1.Select(pos, keyword.Length); pos+; this.Owner.Activate(); /向上查找时 else if (upos = -1) MessageBox.Show(Sorry! 找不到该字符串!); upos = text.Length; else (Form1)this.Owner).RichTextBox1.Text = (Form1)this.Owner).RichTextBox1.Text.Remove(pos, keyword.Length); (Form1)this.Owner).RichTextBox1.Text = (Form1)this.Owner).RichTextBox1.Text.Insert(pos, newword); (Form1)this.Owner).RichTextBox1.Select(upos, keyword.Length); if (upos != 0) upos-; this.Owner.Activate(); /全部替换处理 2.4 文本格式编辑设计2.4.1 自动换行设计bool bo = true; /定义换行条件2.4.2 字体设计private void 字体toolStripButton10_Click(object sender, EventArgs e) FontDialog font = new FontDialog(); if (font.ShowDialog() = DialogResult.OK) this.richTextBox1.SelectionFont = font.Font; 2.4.3 字体颜色设计private void 字体颜色toolStripButton11_Click(object sender, EventArgs e) ColorDialog color = new ColorDialog(); if (color.ShowDialog() = DialogResult.OK) this.richTextBox1.SelectionColor = color.Color; 2.4.4 设计文本左对齐/设置左对齐 private void 左对齐toolStripButton3_Click(object sender, EventArgs e) /利用richTextBox1的属性SelectionAlignment获取或设置应用到当前选定内容或插入点的对齐方式。 /利用HorizontalAlignment 枚举的成员Left,设置对象或文本与控件元素的左侧对齐。 this.richTextBox1.SelectionAlignment = Horizonta
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农行理财考试题库及答案
- 2025年陪诊服务知识题库及答案
- 2025年财务分析师中级专业能力测试题库与答案解析
- 2025年陪诊师资格证考试题库(附答案)
- 2025年文化旅游部公务员招录考试专业知识精讲
- 2025年篮球裁判员测试题及答案
- 2025年酒店物业管理水电维修师笔试模拟试题集及答案解析
- 桡骨小头骨折课件
- 2025年城市设计与可持续发展考试试题及答案
- 2025年篮球教练职业技能认证考试试题及答案
- T/CNCA 048-2023矿用防爆永磁同步伺服电动机通用技术条件
- 微电网短期负荷预测-洞察阐释
- 安装家具合同协议书范本
- 月饼代销合同协议书
- 精神康复与躯体管理训练体系
- 购买肉牛合同协议书
- 移动式压力容器安全技术监察规程(TSG R0005-2011)
- JT-T 495-2025 公路交通安全设施产品质量检验抽样方法
- 《废旧锂电池的回收与再利用》课件
- 2025小学道德与法治教师课标考试模拟试卷附参考答案 (三套)
- 中国卒中患者高血压管理专家共识(2024)解读
评论
0/150
提交评论