【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了“C#课程设计报告(精简版部分代码) - 其它资料“提供大家参考,希望对大家有所帮助!
西安科技大学 《C语言程序设计》
课程设计
报告题 目 简单记事本的
设计院、系部 计算机科学与技术学院专业及班级
计算机网络技术姓 名 牛德洋日 期 2013/1/171 题目要求 使用 C语言设计一个 Windows 自带记事本,软件界面美观大方,操作简洁易用,功能完备可靠,必须上机调试通过。
程序中应有不少于 100300 行的自行编写的
代码,
代码需书写详细注释。
2 功能需求 实现记事本
软件的基本功能,具有文本文件的新建、打开、保存功能,文字的全选、剪切、复制、粘贴、删除、撤销、查找、替换功能,字体类型、格式的设置、显示当前日期与时间的功能,并在此记事本
程序基础上增加设置字体颜色,添加状态栏功能。
3 总体设计4 详细设计 Program.cs:
代码如下 using System using System.Collections.Generic using System.Linq using System.Windows.Forms namespace note static class Program /// ltsummarygt /// 应用程序的主入口点。
/// lt/summarygt STAThread static void Main Application.EnableVisualStyles Application.SetCompatibleTextRenderingDefaultfalse Application.Runnew Form1 Form1.cs
代码如下:using Systemusing System.Collections.Genericusing System.ComponentModelusing System.Datausing System.Drawingusing System.Linqusing System.Textusing System.Windows.Formsusing System.IOnamespace note public partial class Form1 : Form public Form1 InitializeComponent Form2 form2 new Form2//实例化查找窗体 Form3 form3 new Form3//实例化替换窗体 public string filepath quotquot//文件路径 //新建记事本 public void new_Clickobject sender EventArgs e if richTextBox1.Modified//判断输入区是否被修改 save_Clicksender e richTextBox1.Text quotquot//清空输入框 else richTextBox1.Text quotquot //打开文件 public void open_Clickobject sender EventArgs e //“打开”对话框,主要用于打开文件 OpenFileDialog openFileDialog1 new OpenFileDialog //设置打开文件格式 openFileDialog1.Filter quot文本文件.txt.txt所有文件..quot if openFileDialog1.ShowDialog DialogResult.OK //对文件进行读写操作 StreamReader sTmp new StreamReaderopenFileDialog1.FileNameEncoding.Default richTextBox1.Text quotquot this.TextopenFileDialog1.SafeFileNamequot - 记事本quot //读取文件内容 richTextBox1.Text sTmp.ReadToEnd //保存文件 public void save_Clickobject sender EventArgs e //另存为对话框,用于将文本保存到指定的位置 SaveFileDialog saveFileDialog1 new SaveFileDialog //定义要打开的文件格式 saveFileDialog1.Filter quot文本文件.txt.txt所有文件..quot //显示一个quot提示框quot if MessageBox.Showquot需要保存文件吗?quot quot提示框quotMessageBoxButtons.YesNo DialogResult.Yes if saveFileDialog1.ShowDialog DialogResult.OK if filepath.Length 0 //获取要打开文件的绝对路径 filepath saveFileDialog1.FileName StreamWriter sTmp new StreamWriterfilepath sTmp.WriterichTextBox1.Text//输出流 sTmp.Flush//清除缓存 sTmp.Close //另存为 public void saveWhere_Clickobject sender EventArgs e save_Clicksender e //打印 public void print_Clickobject sender EventArgs e //打印对话框,用于选择打印机,打印页数及确定其他与打印机相关的设置 PrintDialog pd new PrintDialog //显示一个模态打印对话框 pd.ShowDialog //退出 public void exit_Clickobject sender EventArgs e save_Clicksender e this.Close//字体public void font_Clickobject sender EventArgs e //字体对话框,用于设置字体 FontDialog fontDialog1 new FontDialog if fontDialog1.ShowDialog DialogResult.OK richTextBox1.Font fontDialog1.Font //打印设置public void set_Clickobject sender EventArgs e PrintDialog pd new PrintDialog pd.ShowDialog//撤消public void return_Clickobject sender EventArgs e //撤消最新的撤消命令。
换句话说,撤消位于撤消堆栈上的最新撤消单元 richTextBox1.Undopublic void 剪切 XToolStripMenuItem_Clickobject sender EventArgs e //从文本编辑控件中删除当前选定内容,并将其复制到 Clipboard richTextBox1.Cutpublic void 全选 AToolStripMenuItem_Clickobject sender EventArgs e //选择文本编辑控件的全部内容。
richTextBox1.SelectAllpublic void 复制 VToolStripMenuItem_Clickobject sender EventArgs e //将文本控件的当前选定内容复制到 Clipboard。
richTextBox1.Copypublic void 粘贴 ToolStripMenuItem_Clickobject sender EventArgs e //将剪贴板的内容粘贴到文本编辑控件中的当前选定内容上。
richTextBox1.Pastepublic void 删除 DToolStripMenuItem_Clickobject sender EventArgs e //清除属性的本地值。
richTextBox1.Clear public void 关于记事本 AToolStripMenuItem_Clickobject sender EventArgse AboutBox1 ab new AboutBox1 ab.ShowDialog public void 查找 FToolStripMenuItem_Clickobject sender EventArgs e form2.rtb richTextBox1 form2.ShowDialogthis public void 替换 RToolStripMenuItem_Clickobject sender EventArgs e form3.rtb richTextBox1 form3.ShowDialogthis public void 时间 ToolStripMenuItem_Clickobject sender EventArgs e //获取
系统当前时间 DateTime time DateTime.Now //格式化日期 string dat string.Formatquot0:yyyy-MM-dd HH:mm:ss ddddquottime richTextBox1.AppendTextdat private void 查找下一个 ToolStripMenuItem_Clickobject sender EventArgse form2.button1_Clicksendere //对“自动换行”进行选定和取消 private void 自动换行 WToolStripMenuItem_Clickobject sender EventArgse if 自动换行 WToolStripMenuItem.Checked richTextBox1.WordWrap false 自动换行 WToolStripMenuItem.Checked false else 自动换行 WToolStripMenuItem.Checked true richTextBox1.WordWrap true //对“状态栏”进行选定和取消 private void 状态栏 ToolStripMenuItem_Clickobject sender EventArgs e if 状态栏 ToolStripMenuItem.Checked statusStrip1.Visible false 状态栏 ToolStripMenuItem.Checked false else 状态栏 ToolStripMenuItem.Checked true statusStrip1.Visible true //对“工具栏”进行选定和取消 private void 工具栏 KToolStripMenuItem_Clickobject sender EventArgs e if 工具栏 KToolStripMenuItem.Checked toolStrip1.Visible false 工具栏 KToolStripMenuItem.Checked false else 工具栏 KToolStripMenuItem.Checked true toolStrip1.Visible true private void 帮助主题 HToolStripMenuItem_Clickobject sender EventArgse MessageBox.Showquot不会使用请百度 Microsoft 记事本quot //设置状态栏的显示时间 private void toolStripStatusLabel1_Clickobject sender EventArgs e toolStripStatusLabel1.Text DateTime.Now.ToString //设置状态栏的行值和列值 private void toolStripStatusLabel3_Clickobject sender EventArgs e toolStripStatusLabel3.Text quot行 quot Control.MousePosition.X quot 列quot Control.MousePosition.Y //设置“编辑”中的按钮是否启用 private void 编辑 ToolStripMenuItem_Clickobject sender EventArgs e if richTextBox1.SelectionLength 0 剪切 XToolStripMenuItem.Enabled true 复制 VToolStripMenuItem.Enabled true 删除 DToolStripMenuItem.Enabled true 查找 FToolStripMenuItem.Enabled true 查找下一个 ToolStripMenuItem.Enabled true else 剪切 XToolStripMenuItem.Enabled false 复制 VToolStripMenuItem.Enabled false 删除 DToolStripMenuItem.Enabled false 查找 FToolStripMenuItem.Enabled false 查找下一个 ToolStripMenuItem.Enabled false if richTextBox1.Modified 撤消 ToolStripMenuItem.Enabled true else 撤消 ToolStripMenuItem.Enabled false //设置“快捷键”中的按钮是否启用 private void contextMenuStrip1_Openingobject sender CancelEventArgs e if richTextBox1.SelectionLength 0 剪切 ToolStripMenuItem.Enabled true 复制 ToolStripMenuItem.Enabled true 删除 ToolStripMenuItem.Enabled true else 剪切 ToolStripMenuItem.Enabled false 复制 ToolStripMenuItem.Enabled false 删除 ToolStripMenuItem.Enabled false if richTextBox1.Modified 撤消 ToolStripMenuItem1.Enabled true else 撤消 ToolStripMenuItem1.Enabled false Form2.cs 的
代码:using Systemusing System.Drawingusing System.Windows.Formsnamespace note public partial class Form2 : Form public Form2 InitializeComponent //实例化一个可输入区域,接收 Form1 中 RichTextBox 的值 public RichTextBox rtb new RichTextBox //定义查找的开始位置 public int startIndex 0 public int flag -1 //“查找下一个”按钮 public void button1_Clickobject sender EventArgs e if startIndex 0 //获取光标的位置 startIndex rtb.SelectionStart if this.checkBox1.Checked //向下查找 if this.radioButton2.Checked //索引:CurrentCulture 使用区域敏感排序规则和当前区域比较字符串。
//CurrentCultureIgnoreCase 使用区域敏感排序规则、当前区域来比较字符串, //同时忽略被比较字符串的大小写。
flag rtb.Text.IndexOftextBox1.Text startIndexStringComparison.CurrentCulture //向上查找 if this.radioButton1.Checked ampamp startIndexgt2 flag rtb.Text.LastIndexOftextBox1.Text startIndex-2StringComparison.CurrentCulture else if this.radioButton2.Checked flag rtb.Text.IndexOftextBox1.Text startIndexStringComparison.CurrentCultureIgnoreCase if this.radioButton1.Checked ampamp startIndex gt 2 flag rtb.Text.LastIndexOftextBox1.Text startIndex-2StringComparison.CurrentCultureIgnoreCase if flag gt -1 //设置选中字体的背景颜色 rtb.SelectionBackColor rtb.BackColor startIndex flag1 rtb.SelectionStart flag flag -1 //获取选中字体的长度 rtb.SelectionLength textBox1.Text.Length rtb.SelectionBackColor Color.SkyBlue else MessageBox.Showquot找不到quotquot textBox1.Text quotquotquotquot提示quot //销毁当前窗体 public void button2_Clickobject sender EventArgs e this.Close //设置查找按钮是否启用 public void textBox1_TextChangedobject sender EventArgs e if textBox1.Text quotquot button1.Enabled true//启用 else button1.Enabled false//禁用 Form3.cs 的
代码:using Systemusing System.Collections.Genericusing System.ComponentModelusing System.Datausing System.Drawingusing System.Linqusing System.Textusing System.Windows.Formsnamespace note public partial class Form3 : Form public Form3 InitializeComponent Form2 form2 new Form2 //实例化一个可输入区域,接收 Form1 中 RichTextBox 的值 public RichTextBox rtb new RichTextBox //定义查找的开始位置 public int startIndex 0 public int flag -1 //设置查找,替换按钮是否启用 public void textBox1_TextChangedobject sender EventArgs e startIndex rtb.SelectionLength if textBox1.Text quotquot button1.Enabled true button2.Enabled true button3.Enabled true else button1.Enabled false button2.Enabled false button3.Enabled false public void button1_Clickobject sender EventArgs e if this.checkBox1.Checked flag rtb.Text.IndexOftextBox1.Text startIndexStringComparison.CurrentCulture else flag rtb.Text.IndexOftextBox1.Text startIndexStringComparison.CurrentCultureIgnoreCase if flag gt -1 rtb.SelectionBackColor rtb.BackColor startIndex flag 1 rtb.SelectionStart flag flag -1 rtb.SelectionLength textBox1.Text.Length rtb.SelectionBackColor Color.SkyBlue else MessageBox.Showquot找不到quotquot textBox1.Text quotquotquot quot提示quot //替换按钮的方法 private void button2_Clickobject sender EventArgs e if rtb.SelectedText quotquot button1_Clicksender e else rtb.SelectionBackColor rtb.BackColor rtb.SelectedText textBox2.Text button1_Clicksender e //销毁当前窗体 private void button4_Clickobject sender EventArgs e this.Close //全部替换按钮事件 private void button3_Clickobject sender EventArgs e startIndex 0 whilestartIndex ltrtb.Text.Length //清除选中的字体的背景色 rtb.SelectionBackColor rtb.BackColor rtb.SelectedText textBox2.Text button1_Clicksender e if startIndex r.