【VB开源代码栏目提醒】:网学会员在VB开源代码频道为大家收集整理了“C#_编写记事本源代码 - 其它资料“提供大家参考,希望对大家有所帮助!
WINDOWS 编程 实验
报告 4实验序号: 实验项目名称: windows 界面设计与文件读取(自己编写一个记事本) 学 号 1009030134 姓 名 王仲春 专业、班 信管 1101实验地点 文波 311 指导教师 李毅鹏 时间 2012/12/05一、 实验目的及要求 掌握窗体、常用控件与组件的添加和
代码编写; 实现目录管理、文件的读写。
二、实验设备(环境)及要求 Windows Xp 操作
系统; C集成开发环境; .net 开发框架;二、 实验内容与结果 实验内容: 编写一个记事本,实现文件的打开,保存,编辑,和文字的格式调整等功能; 其中包含了窗体、
常用控件与组件的添加和
代码编写; 实现了目录管理和文件的读写。
实验结果: 1.运行界面或实验截图: 记事本运行效果: (总体效果)(字体格式效果)(文件保存按钮效果)(“帮助按钮运行效果”)设计过程截图: (各类组件)(链接窗体“关于此记事本”的
设计) (发布操作截图)2.记事本源
代码: using System using System.Collections.Generic using System.ComponentModel using System.Data using System.Drawing using System.Text using System.Windows.Forms namespace NotePad //namespace命¨名空间,ê以°便加¨以°识别e public partial class frmNotepad : Form //public 表¨示此类¤¨¤为a公开a类¤¨¤,ê可¨以°在¨其他文件t其他 页°3面使1用;ê //partial是局部类¤¨¤标¨记,ê用于¨需¨¨要°a多¨¤个局部类¤¨¤的文件t 中D //解a决可¨能¨需¨¨要°a多¨¤人¨编¨¤写多¨¤个类¤¨¤或¨°者类¤¨¤很¨大¨;ê //但终究各局部类¤¨¤还1是要°a集成¨;ê //:êoform表¨示新建§的frm是继¨承D环¤境3已°有D类¤¨¤的;ê public frmNotepad InitializeComponent //初始化窗体;ê只能¨写在¨文件t前面;ê private void frmNotepad_SizeChangedobject sender EventArgs e frmNotepad ob_frmNotepad new frmNotepad tssLbl1.Width this.Width / 2 - 12 tssLbl2.Width tssLbl1.Width //窗体大¨小;ê bool b false bool s true //b为atrue表¨示文件t是磁盘¨打¨°开a的;ê为afalse表¨示文件t是新建§的;ê //s为atrue表¨示文件t已°保ê存;ê反¤之未保ê存;ê private void rtxtNotepad_TextChangedobject sender EventArgs e s false //文件t已°被修T改sfalse 文件t未保ê存;ê //TextChange事件t当text被改变事促¨发¤é;ê private void tsmiNew_Clickobject sender EventArgs e if b true rtxtNotepad.Text.Trim string result result MessageBox.Show文件t还1未保ê存!ê是否¤保ê存 保ê存文件t MessageBoxButtons.YesNoCancel.ToString switch result case Yes: if b true rtxtNotepad.SaveFileodlgNotepad.FileName else if sdlgNotepad.ShowDialog DialogResult.OK rtxtNotepad.SaveFilesdlgNotepad.FileName s true rtxtNotepad.Text break case No: b false rtxtNotepad.Text break //新建§按钮¤的功能¨:êo此时s为afalse,ê即文件t未保ê存,ê若¨需¨¨保ê存,ê则¨°调用保ê存类¤¨¤Savefile,ê //若¨为a打¨°开a的文件t,ê则¨°保ê存到原-文件t夹D,ê若¨是新建§的,ê一°般保ê存;ê private void tsmiOpen_Clickobject sender EventArgs e if b true rtxtNotepad.Text.Trim if s false string result result MessageBox.Show文件t还1未保ê存!ê是否¤保ê存 保ê存文件t MessageBoxButtons.YesNoCancel.ToString switch result case Yes: if b true rtxtNotepad.SaveFileodlgNotepad.FileName else if sdlgNotepad.ShowDialog DialogResult.OK rtxtNotepad.SaveFilesdlgNotepad.FileName s true break case No: b false rtxtNotepad.Text break odlgNotepad.RestoreDirectory true if odlgNotepad.ShowDialog DialogResult.OK odlgNotepad.FileName rtxtNotepad.LoadFileodlgNotepad.FileName b true s true //打¨°开a;ê private void tsmiSave_Clickobject sender EventArgs e if b true rtxtNotepad.Modified true rtxtNotepad.SaveFileodlgNotepad.FileName s true else if b false rtxtNotepad.Text.Trim sdlgNotepad.ShowDialog DialogResult.OK rtxtNotepad.SaveFilesdlgNotepad.FileName b true s true odlgNotepad.FileName sdlgNotepad.FileName //保ê存;ê分¤为a打¨°开a文件t和¨新建§类¤¨¤型¨;ê private void tsmiSaveAs_Clickobject sender EventArgs e if sdlgNotepad.ShowDialog DialogResult.OK rtxtNotepad.SaveFilesdlgNotepad.FileName s true //另¨存为a;ê private void tsmiClose_Clickobject sender EventArgs e Application.Exit //退出;ê private void tsmiUndo_Clickobject sender EventArgs e rtxtNotepad.Undo//撤¤销¨;êprivate void tsmiCopy_Clickobject sender EventArgs e rtxtNotepad.Copy//复制;êprivate void tsmiCut_Clickobject sender EventArgs e rtxtNotepad.Cut//剪切D;êprivate void tsmiPaste_Clickobject sender EventArgs e rtxtNotepad.Paste//粘3贴¨;êprivate void tsmiSelectAll_Clickobject sender EventArgs e rtxtNotepad.SelectAll//全¨选;êprivate void tsmiDate_Clickobject sender EventArgs e rtxtNotepad.AppendTextSystem.DateTime.Now.ToString//插入¨时间;êprivate void tsmiFont_Clickobject sender EventArgs e fdlgNotepad.ShowColor true if fdlgNotepad.ShowDialog DialogResult.OK rtxtNotepad.SelectionColor fdlgNotepad.Color rtxtNotepad.SelectionFont fdlgNotepad.Font //调节¨颜色private void tsmiAuto_Clickobject sender EventArgs e if tsmiAuto.Checked false tsmiAuto.Checked true rtxtNotepad.WordWrap true else tsmiAuto.Checked false rtxtNotepad.WordWrap false //自动换行D;ê点击菜单¤式执行D;êprivate void tsmiToolStrip_Clickobject sender EventArgs e Point point if tsmiToolStrip.Checked true point new Point0 24 tsmiToolStrip.Checked false tlsNotepad.Visible false //visible可¨见性;ê rtxtNotepad.Location point //此处location指的是左¨上角的位置;ê rtxtNotepad.Height tlsNotepad.Height else point new Point0 49 tsmiToolStrip.Checked true tlsNotepad.Visible true rtxtNotepad.Location point rtxtNotepad.Height - tlsNotepad.Height //显示菜单¤栏¤与否¤;êprivate void tsmiStatusStrip_Clickobject sender EventArgs e if tsmiStatusStrip.Checked true tsmiStatusStrip.Checked false stsNotepad.Visible false rtxtNotepad.Height stsNotepad.Height else tsmiStatusStrip.Checked true stsNotepad.Visible true rtxtNotepad.Height - stsNotepad.Height //状态栏¤;êprivate void tsmiAbout_Clickobject sender EventArgs e frmAbout ob_FrmAbout new frmAbout ob_FrmAbout.Show//关于¨记事本;ê//以°下是图标¨栏¤按钮¤代¨码;êprivate void tsbCut_Clickobject sender EventArgs e rtxtNotepad.Cut//剪切D;êprivate void tsbCopy_Clickobject sender EventArgs e rtxtNotepad.Copy//复制;êprivate void tsbPaste_Clickobject sender EventArgs e rtxtNotepad.Paste //粘3贴¨;ê private void tsbUndo_Clickobject sender EventArgs e rtxtNotepad.Undo //撤¤销¨;ê private void tsbSave_Clickobject sender EventArgs e if b true rtxtNotepad.Modified true rtxtNotepad.SaveFileodlgNotepad.FileName s true else if b false rtxtNotepad.Text.Trim sdlgNotepad.ShowDialog DialogResult.OK rtxtNotepad.SaveFilesdlgNotepad.FileName b true s true odlgNotepad.FileName sdlgNotepad.FileName //保ê存;ê private void tsbOpen_Clickobject sender EventArgs e if b true rtxtNotepad.Text.Trim if s false string result result MessageBox.Show文件t还1未保ê存!ê是否¤保ê存 保ê存文件t MessageBoxButtons.YesNoCancel.ToString switch result case Yes: if b true rtxtNotepad.SaveFileodlgNotepad.FileName else if sdlgNotepad.ShowDialog DialogResult.OK rtxtNotepad.SaveFilesdlgNotepad.FileName s true break case No: b false rtxtNotepad.Text break odlgNotepad.RestoreDirectory true if odlgNotepad.ShowDialog DialogResult.OK odlgNotepad.FileName rtxtNotepad.LoadFileodlgNotepad.FileName b true s true //打¨°开a;ê private void tsbNew_Clickobject sender EventArgs e if b true rtxtNotepad.Text.Trim string result result MessageBox.Show文件t还1未保ê存!ê是否¤保ê存 保ê存文件t MessageBoxButtons.YesNoCancel.ToString switch result case Yes: if b true rtxtNotepad.SaveFileodlgNotepad.FileName else if sdlgNotepad.ShowDialog DialogResult.OK rtxtNotepad.SaveFilesdlgNotepad.FileName s true rtxtNotepad.Text break case No: b false rtxtNotepad.Text break //新建§;ê private void tsbFont_Clickobject sender EventArgs e fdlgNotepad.ShowColor true if fdlgNotepad.ShowDialog DialogResult.OK rtxtNotepad.SelectionColor fdlgNotepad.Color rtxtNotepad.SelectionFont fdlgNotepad.Font private void tsbAbout_Clickobject sender EventArgs e frmAbout ob_FrmAbout new frmAbout ob_FrmAbout.Show private void tsmiEdit_Clickobject sender EventArgs e //调整字体;ê 三、 分析与讨论 通过此次实验,掌握了记事本的开发过程; 熟悉了各类组件控件的使用; 联系了
软件开发(界面设计,功能
代码编写,生成可执行文件,发布成可安装文 件……)的基础步骤;六、教师评语 成绩 签名: 日期: