C#实现简单记事本程序
程序员文章站
2023-11-10 08:49:28
本文实例为大家分享了使用c#写出一个简单的记事本程序,供大家参考,具体内容如下
编程语言: c#
编程环境: visual studio 2013
运行环境: ....
本文实例为大家分享了使用c#写出一个简单的记事本程序,供大家参考,具体内容如下
编程语言: c#
编程环境: visual studio 2013
运行环境: .net framework 4.5
预览:
功能:
标题栏
显示文件标题
菜单栏
各类菜单命令
文件- 新建
- 打开
- 保存
- 另存为
- 页面设置
- 打印
- 退出
编辑
- 撤销
- 剪切
- 复制
- 粘贴
- 全选
- 时间/日期
格式
- 自动换行
- 字体
视图
- 状态栏
- 工具栏
- 全屏模式
帮助
- 开源许可
- 查看帮助
- 关于
工具栏
常用工具集合
标签栏
文件标签显示
工作区
编辑区
状态栏
显示文件状态
文本状态(新建/已修改)
- 字符个数
- 光标坐标
- 功能实现
本程序有两个窗体,分别为form1和aboutbox1
所有文件如下:
对于form1:
需要引入的类库:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.io; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;
窗体及空间声明代码:
private system.windows.forms.menustrip menustrip1; private system.windows.forms.toolstripmenuitem 文件toolstripmenuitem; private system.windows.forms.toolstripmenuitem 新建toolstripmenuitem; private system.windows.forms.toolstripmenuitem 打开toolstripmenuitem; private system.windows.forms.toolstripmenuitem 保存toolstripmenuitem; private system.windows.forms.toolstripmenuitem 另存为toolstripmenuitem; private system.windows.forms.toolstripmenuitem 编辑toolstripmenuitem; private system.windows.forms.toolstripmenuitem 格式toolstripmenuitem; private system.windows.forms.toolstripmenuitem 退出toolstripmenuitem; private system.windows.forms.toolstripmenuitem 关于toolstripmenuitem; private system.windows.forms.textbox editbox1; private system.windows.forms.toolstripmenuitem 撤销toolstripmenuitem; private system.windows.forms.toolstripmenuitem 剪切toolstripmenuitem; private system.windows.forms.toolstripmenuitem 复制toolstripmenuitem; private system.windows.forms.toolstripmenuitem 粘贴toolstripmenuitem; private system.windows.forms.toolstripmenuitem 删除toolstripmenuitem; private system.windows.forms.toolstripmenuitem 全选atoolstripmenuitem; private system.windows.forms.toolstripmenuitem 日期dtoolstripmenuitem; private system.windows.forms.toolstripmenuitem 格式toolstripmenuitem1; private system.windows.forms.toolstripmenuitem 字体toolstripmenuitem; private system.windows.forms.toolstripmenuitem 查看vtoolstripmenuitem; private system.windows.forms.toolstripmenuitem 状态栏toolstripmenuitem; private system.windows.forms.statusstrip statusstrip1; private system.windows.forms.savefiledialog savefiledialog1; private system.windows.forms.toolstripseparator toolstripmenuitem1; private system.windows.forms.toolstripseparator toolstripmenuitem3; private system.windows.forms.toolstripseparator toolstripmenuitem4; private system.windows.forms.toolstripmenuitem 页面设置utoolstripmenuitem; private system.drawing.printing.printdocument printdocument1; private system.windows.forms.pagesetupdialog pagesetupdialog1; private system.windows.forms.toolstripmenuitem 打印ptoolstripmenuitem; private system.windows.forms.printdialog printdialog1; private system.windows.forms.toolstripmenuitem 查看帮助htoolstripmenuitem; private system.windows.forms.toolstripseparator toolstripmenuitem5; private system.windows.forms.toolstripmenuitem 自动换行toolstripmenuitem; private system.windows.forms.toolstripstatuslabel toolstripstatuslabel1; public system.windows.forms.timer timer1; private system.windows.forms.toolstripstatuslabel toolstripstatuslabel2; private system.windows.forms.toolstripstatuslabel toolstripstatuslabel3; private system.windows.forms.toolstripmenuitem 全屏模式toolstripmenuitem; private system.windows.forms.toolstrip toolstrip1; private system.windows.forms.toolstripbutton newbutton; private system.windows.forms.toolstripbutton openbutton; private system.windows.forms.toolstripbutton savebutton; private system.windows.forms.toolstripbutton saveasbutton; private system.windows.forms.toolstripseparator toolstripseparator1; private system.windows.forms.toolstripbutton cutbutton; private system.windows.forms.toolstripbutton copybutton; private system.windows.forms.toolstripbutton pastebutton; private system.windows.forms.toolstripbutton deletebutton; private system.windows.forms.toolstripseparator toolstripseparator2; private system.windows.forms.toolstripbutton timebutton; private system.windows.forms.toolstripbutton fullbutton; private system.windows.forms.toolstripbutton textbutton; private system.windows.forms.toolstripmenuitem 工具栏toolstripmenuitem; private system.windows.forms.toolstripstatuslabel toolstripstatuslabel4; private system.windows.forms.toolstripmenuitem 开源许可otoolstripmenuitem; private system.windows.forms.tabcontrol tabcontrol1; private system.windows.forms.tabpage tabpage1; private system.windows.forms.tabpage tabpage2; private system.windows.forms.textbox editbox2;1
窗体载入:
private void form1_load(object sender, eventargs e) { this.editbox1.scrollbars = scrollbars.both; this.editbox2.scrollbars = scrollbars.both; this.text = textfilename + " - " + programename;//显示文件名 this.timer1.start(); editbox1.focus(); }
首先进行一些固定变量声明:
private string textfilename = "无标题"; private string programename = "icey"; private string filepath = ""; private string asfilepath = ""; private string selectetext = ""; private string helpurl = "https://blog.mayuko.cn/icey"; private string opensourceurl = "https://github.com/mayuko2012/icey"; private string wrongmessage = "你好像遇到了错误..."; private string fileformat = "文本文件(*.txt)|*.txt|icey文件(*.ice)|*.ice|c++文件(*.cpp)|*.cpp|c文件(*.c)|*.c|所有文件(*.*)|(*.*)"; private string tabfilename1 = "无标题1 - icey"; private string tabfilename2 = "无标题2 - icey"; boolean savefilestatus1 = false; boolean textchanged1 = false; boolean savefilestatus2 = false; boolean textchanged2 = false;
保存文件采用函数形式:
private void savefile()//保存 { if (!textfilename.equals("")) { savefiledialog savefile = new savefiledialog(); savefile.filter = fileformat; savefile.filename = "*.txt"; if (savefile.showdialog() == dialogresult.ok) { filepath = savefile.filename; streamwriter sw = new streamwriter(filepath, false, encoding.default); sw.write((editbox1.focused) ? editbox1.text : editbox2.text); sw.close(); if (editbox1.focused) { tabfilename1 = savefile.filename + " - " + programename; savefilestatus1 = true; } else if (editbox2.focused) { tabfilename2 = savefile.filename + " - " + programename; savefilestatus2 = true; } } } toolstripstatuslabel4.text = "已保存"; }
另存为采用函数形式:
private void saveasfile()//另存为 { savefiledialog saveasfile = new savefiledialog(); saveasfile.filter = fileformat; saveasfile.filename = "*.txt"; if (saveasfile.showdialog() == dialogresult.ok) { asfilepath = saveasfile.filename; streamwriter sw = new streamwriter(asfilepath, false, encoding.default); sw.writeline((editbox1.focused) ? editbox1.text : editbox2.text); sw.close(); fileinfo fileinfo = new fileinfo(saveasfile.filename); textfilename = fileinfo.name; } toolstripstatuslabel4.text = "已保存"; }
新建函数:
private void newfile()//新建 { if (editbox1.focused) { if (editbox1.text != string.empty && savefilestatus1 == false && textchanged1 == true)//如果文本框不为空 { dialogresult result = messagebox.show("是否将窗口1已编辑文件保存到 " + textfilename, wrongmessage, messageboxbuttons.yesnocancel, messageboxicon.information); if (result == dialogresult.yes) { savefile(); application.exit(); } else if (result == dialogresult.no) { editbox1.text = ""; } } else editbox1.text = ""; } else if (editbox2.focused) { if (editbox2.text != string.empty && savefilestatus2 == false && textchanged2 == true)//如果文本框不为空 { dialogresult result = messagebox.show("是否将窗口2已编辑文件保存到 " + textfilename, wrongmessage, messageboxbuttons.yesnocancel, messageboxicon.information); if (result == dialogresult.yes) { savefile(); application.exit(); } else if (result == dialogresult.no) { editbox2.text = ""; } } else editbox2.text = ""; } }
打开文件函数:
private void openfile()//打开 { openfiledialog openfile = new openfiledialog(); openfile.filter = fileformat; if (openfile.showdialog() == dialogresult.ok) { streamreader sr = new streamreader(openfile.filename, encoding.default); if (editbox1.focused) { editbox1.text = sr.readtoend(); } else if(editbox2.focused) { editbox2.text = sr.readtoend(); } sr.close(); fileinfo fileinfo = new fileinfo(openfile.filename); if (editbox1.focused) { tabfilename1 = fileinfo.name + " - " + programename; savefilestatus1 = true; } else if (editbox2.focused) { tabfilename2 = fileinfo.name + " - " + programename; savefilestatus2 = true; } textfilename = fileinfo.name; } }
全屏模式函数:
private void fullscreen()//全屏 { if (全屏模式toolstripmenuitem.checked == false) { this.windowstate = formwindowstate.maximized; 全屏模式toolstripmenuitem.checked = true; this.formborderstyle = system.windows.forms.formborderstyle.none; } else { this.formborderstyle = system.windows.forms.formborderstyle.sizable; this.windowstate = formwindowstate.normal; 全屏模式toolstripmenuitem.checked = false; } }
退出菜单命令:
private void 退出toolstripmenuitem_click(object sender, eventargs e) { if (editbox1.text != string.empty || savefilestatus1 == false && textchanged1 == true) { this.tabpage1.show(); this.editbox1.focus(); dialogresult result = messagebox.show("是否将窗口1已编辑文件保存到 " + textfilename, wrongmessage, messageboxbuttons.yesnocancel, messageboxicon.information); if (result == dialogresult.yes) { savefile(); application.exit(); } else if (result == dialogresult.no) { application.exit(); } } else if (editbox2.text != string.empty || savefilestatus2 == false && textchanged2 == true) { this.tabpage2.show(); this.editbox2.focus(); dialogresult result = messagebox.show("是否将窗口2已编辑文件保存到 " + textfilename, wrongmessage, messageboxbuttons.yesnocancel, messageboxicon.information); if (result == dialogresult.yes) { savefile(); application.exit(); } else if (result == dialogresult.no) { application.exit(); } } else application.exit(); }
bool变量,用于判断textbox是否发生变化:
private void textbox1_textchanged(object sender, eventargs e) { textchanged2 = true; toolstripstatuslabel4.text = "已修改"; } private void editbox2_textchanged(object sender, eventargs e) { textchanged2 = true; toolstripstatuslabel4.text = "已修改"; }
新建菜单命令:
private void 新建toolstripmenuitem_click(object sender, eventargs e) { newfile(); }
打开菜单命令:
private void 打开toolstripmenuitem_click(object sender, eventargs e) { openfile(); }
字体菜单命令:
private void 字体toolstripmenuitem_click(object sender, eventargs e) { fontdialog fontdialog = new fontdialog(); if (fontdialog.showdialog() == dialogresult.ok) { if (editbox1.focused) { editbox1.font = fontdialog.font; } else editbox2.font = fontdialog.font; } }
退出动作(当用户点击窗体右上角退出按钮时执行此操作):
private void form1_formclosing(object sender, formclosingeventargs e) { if (editbox1.text != string.empty && e.closereason == closereason.userclosing || savefilestatus1 == false && textchanged1 == true)//如果文本框不为空&&触发关闭按钮事件 { this.tabpage1.show(); this.editbox1.focus(); dialogresult result = messagebox.show("是否将窗体1已编辑文件保存到 " + textfilename, wrongmessage, messageboxbuttons.yesnocancel, messageboxicon.information); if (result == dialogresult.yes) { savefile(); e.cancel = false; } else if (result == dialogresult.no) { e.cancel = false; } else if (result == dialogresult.cancel) { e.cancel = true; } } else if (editbox2.text != string.empty && e.closereason == closereason.userclosing || savefilestatus2 == false && textchanged2 == true)//如果文本框不为空&&触发关闭按钮事件 { this.tabpage2.show(); this.editbox2.focus(); dialogresult result = messagebox.show("是否将窗口2已编辑文件保存到 " + textfilename, wrongmessage, messageboxbuttons.yesnocancel, messageboxicon.information); if (result == dialogresult.yes) { savefile(); e.cancel = false; } else if (result == dialogresult.no) { e.cancel = false; } else if (result == dialogresult.cancel) { e.cancel = true; } } else application.exit(); }
状态栏命令(状态栏是否显示):
private void 状态栏toolstripmenuitem_click(object sender, eventargs e) { if (状态栏toolstripmenuitem.checked == true) { 状态栏toolstripmenuitem.checked = false; statusstrip1.visible = false; } else { 状态栏toolstripmenuitem.checked = true; statusstrip1.visible = true; } }
编辑命令:
private void 编辑toolstripmenuitem_click(object sender, eventargs e) { if ((editbox1.selectedtext.equals(""))) { 剪切toolstripmenuitem.enabled = false; 复制toolstripmenuitem.enabled = false; 删除toolstripmenuitem.enabled = false; } else { 剪切toolstripmenuitem.enabled = true; 复制toolstripmenuitem.enabled = true; 删除toolstripmenuitem.enabled = true; } }
全选命令:
private void 全选atoolstripmenuitem_click(object sender, eventargs e) { if (editbox1.focused) { this.editbox1.selectall(); } else this.editbox2.selectall(); }
剪切 复制 粘贴 删除命令:
private void 剪切toolstripmenuitem_click(object sender, eventargs e) { if (editbox1.focused) { selectetext = editbox1.selectedtext; this.editbox1.cut(); } else { selectetext = editbox2.selectedtext; this.editbox2.cut(); } } private void 撤销toolstripmenuitem_click(object sender, eventargs e) { if (editbox1.focused) { this.editbox1.undo(); } else this.editbox2.undo(); } private void 复制toolstripmenuitem_click(object sender, eventargs e) { if (editbox1.focused) { this.editbox1.copy(); } else this.editbox2.copy(); } private void 粘贴toolstripmenuitem_click(object sender, eventargs e) { if (editbox1.focused) { this.editbox1.paste(); } else this.editbox2.paste(); } private void 删除toolstripmenuitem_click(object sender, eventargs e) { if (editbox1.focused) { this.editbox1.selectedtext = ""; } else this.editbox2.selectedtext = ""; }
保存命令:
private void 保存toolstripmenuitem_click(object sender, eventargs e) { savefile(); }
另存为命令:
private void 另存为toolstripmenuitem_click(object sender, eventargs e) { saveasfile(); }
时间戳命令:
private void 日期dtoolstripmenuitem_click(object sender, eventargs e) { if (editbox1.focused) { editbox1.appendtext(system.datetime.now.tostring()); } else editbox2.appendtext(system.datetime.now.tostring()); }
页面设置命令:
private void 页面设置utoolstripmenuitem_click(object sender, eventargs e) { pagesetupdialog1.document = printdocument1; this.pagesetupdialog1.allowmargins = true; this.pagesetupdialog1.alloworientation = true; this.pagesetupdialog1.allowpaper = true; this.pagesetupdialog1.allowprinter = true; this.pagesetupdialog1.document = this.printdocument1; pagesetupdialog1.showdialog(); }
打印命令:
private void 打印ptoolstripmenuitem_click(object sender, eventargs e) { this.printdialog1.document = this.printdocument1; this.printdialog1.printersettings = this.pagesetupdialog1.printersettings; if (this.printdialog1.showdialog() == dialogresult.ok) { try { this.printdocument1.print(); } catch (exception ex) { messagebox.show(ex.message, wrongmessage, messageboxbuttons.ok, messageboxicon.error); } } }
查看帮助 关于命令:
private void 查看帮助htoolstripmenuitem_click(object sender, eventargs e) { system.diagnostics.process.start(helpurl); } private void 关于toolstripmenuitem_click(object sender, eventargs e) { aboutbox1 about = new aboutbox1(); about.startposition = formstartposition.centerscreen; about.show(); about.owner = this; //timer1.stop(); }
自动换行命令:
private void 自动换行toolstripmenuitem_click(object sender, eventargs e) { if (自动换行toolstripmenuitem.checked == true) { editbox1.wordwrap = false; editbox2.wordwrap = false; 自动换行toolstripmenuitem.checked = false; } else { editbox1.wordwrap = true; editbox2.wordwrap = true; 自动换行toolstripmenuitem.checked = true; } }
计时器(100 ms刷新频率):
private void timer1_tick(object sender, eventargs e) { toolstripstatuslabel1.text = (editbox1.focused) ? editbox1.text.length.tostring() + " 个字符" : editbox2.text.length.tostring() + " 个字符"; int totalline = (editbox1.focused) ? editbox1.getlinefromcharindex(editbox1.text.length) + 1 : editbox2.getlinefromcharindex(editbox2.text.length) + 1;//得到总行数 int index = (editbox1.focused) ? editbox1.getfirstcharindexofcurrentline() : editbox2.getfirstcharindexofcurrentline();//得到当前行第一个字符的索引 int line = (editbox1.focused) ? editbox1.getlinefromcharindex(index) + 1 : editbox2.getlinefromcharindex(index) + 1;//得到当前行的行号 int col = (editbox1.focused) ? editbox1.selectionstart - index + 1 : editbox2.selectionstart - index + 1;//.selectionstart得到光标所在位置的索引 - 当前行第一个字符的索引 = 光标所在的列数 toolstripstatuslabel2.text = "第" + line + "行,第" + col + "列"; if (( (editbox1.focused) ? editbox1.selectedtext.equals(""):editbox2.selectedtext.equals(""))) { cutbutton.enabled = false; copybutton.enabled = false; deletebutton.enabled = false; } else { cutbutton.enabled = true; copybutton.enabled = true; deletebutton.enabled = true; } if (editbox1.focused) { editbox1.focus(); this.text = tabfilename1; } else { editbox2.focus(); this.text = tabfilename2; } if (editbox2.focused) { editbox2.focus(); this.text = tabfilename2; } else { editbox1.focus(); this.text = tabfilename1; } }
工具栏命令(工具栏是否显示):
private void 工具栏toolstripmenuitem_click(object sender, eventargs e) { if (工具栏toolstripmenuitem.checked == false) { toolstrip1.visible = true; 工具栏toolstripmenuitem.checked = true; } else if(工具栏toolstripmenuitem.checked == true) { toolstrip1.visible = false; 工具栏toolstripmenuitem.checked = false; } }
开源许可命令:
private void 开源许可otoolstripmenuitem_click(object sender, eventargs e) { system.diagnostics.process.start(opensourceurl); }
工具栏的各个按钮:
private void newbutton_click(object sender, eventargs e) { newfile(); } private void openbutton_click(object sender, eventargs e) { openfile(); } private void savebutton_click(object sender, eventargs e) { savefile(); } private void saveasbutton_click(object sender, eventargs e) { saveasfile(); } private void cutbutton_click(object sender, eventargs e) { if (editbox1.focused) { selectetext = editbox1.selectedtext; this.editbox1.cut(); } else { selectetext = editbox2.selectedtext; this.editbox2.cut(); } } private void copybutton_click(object sender, eventargs e) { if (editbox1.focused) { this.editbox1.copy(); } else this.editbox2.copy(); } private void pastebutton_click(object sender, eventargs e) { if (editbox1.focused) { this.editbox1.paste(); } else this.editbox2.paste(); } private void deletebutton_click(object sender, eventargs e) { if (editbox1.focused) { this.editbox1.selectedtext = ""; } else this.editbox2.selectedtext = ""; } private void timebutton_click(object sender, eventargs e) { if (editbox1.focused) { editbox1.appendtext(system.datetime.now.tostring()); } else editbox2.appendtext(system.datetime.now.tostring()); } private void textbutton_click(object sender, eventargs e) { fontdialog fontdialog = new fontdialog(); if (fontdialog.showdialog() == dialogresult.ok) { if (editbox1.focused) { editbox1.font = fontdialog.font; } else editbox2.font = fontdialog.font; } } private void fullbutton_click(object sender, eventargs e) { fullscreen(); }
标签栏:
private void tabpage1_click(object sender, eventargs e) { editbox1.focus(); this.text = tabpage1.text; if (textchanged1 == false) { toolstripstatuslabel4.text = "新建"; } } private void tabpage2_click(object sender, eventargs e) { editbox2.focus(); this.text = tabpage2.text; if (textchanged2 == false) { toolstripstatuslabel4.text = "新建"; } }
对于aboutbox:
using system; using system.collections.generic; using system.componentmodel; using system.drawing; using system.linq; using system.reflection; using system.threading.tasks; using system.windows.forms; namespace notepad { partial class aboutbox1 : form { public aboutbox1() { initializecomponent(); this.text = string.format("关于 {0}", assemblytitle); this.labelproductname.text = assemblyproduct; this.labelversion.text = string.format("版本 {0}", assemblyversion); this.labelcopyright.text = assemblycopyright; this.labelcompanyname.text = assemblycompany; this.textboxdescription.text = assemblydescription; } #region 程序集特性访问器 public string assemblytitle { get { object[] attributes = assembly.getexecutingassembly().getcustomattributes(typeof(assemblytitleattribute), false); if (attributes.length > 0) { assemblytitleattribute titleattribute = (assemblytitleattribute)attributes[0]; if (titleattribute.title != "") { return titleattribute.title; } } return system.io.path.getfilenamewithoutextension(assembly.getexecutingassembly().codebase); } } public string assemblyversion { get { return assembly.getexecutingassembly().getname().version.tostring(); } } public string assemblydescription { get { object[] attributes = assembly.getexecutingassembly().getcustomattributes(typeof(assemblydescriptionattribute), false); if (attributes.length == 0) { return ""; } return ((assemblydescriptionattribute)attributes[0]).description; } } public string assemblyproduct { get { object[] attributes = assembly.getexecutingassembly().getcustomattributes(typeof(assemblyproductattribute), false); if (attributes.length == 0) { return ""; } return ((assemblyproductattribute)attributes[0]).product; } } public string assemblycopyright { get { object[] attributes = assembly.getexecutingassembly().getcustomattributes(typeof(assemblycopyrightattribute), false); if (attributes.length == 0) { return ""; } return ((assemblycopyrightattribute)attributes[0]).copyright; } } public string assemblycompany { get { object[] attributes = assembly.getexecutingassembly().getcustomattributes(typeof(assemblycompanyattribute), false); if (attributes.length == 0) { return ""; } return ((assemblycompanyattribute)attributes[0]).company; } } #endregion private void aboutbox1_load(object sender, eventargs e) { } private void okbutton_click(object sender, eventargs e) { messagebox.show("已是最新版本!", "检查更新"); } private void aboutbox1_formclosing(object sender, formclosingeventargs e) { form1 frm1 = (form1)this.owner; frm1.timer1.start(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。