C#记事本

这是一个使用C#编写的简单记事本程序,包含新建、打开、保存、打印预览、查找、替换等功能。源代码注释详细,涵盖了文件操作、文本编辑、界面交互等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. <font size="4" color="#ff0000"><strong><u>没找到上传压缩文件的按钮,只好把源代码上传到这里了.<img alt="" src="/Editor/FCKeditor/editor/images/smiley/msn/regular_smile.gif"></u></strong></font>  
没找到上传压缩文件的按钮,只好把源代码上传到这里了.
  1. <strong><u><font size="4" color="#ff0000">注释很详细哦</font></u></strong>  
注释很详细哦
  1. <font size="4" color="#ff0000"><strong><u>Form1.cs   </u></strong></font><strong><u><font size="4" color="#ff0000">************************************************</font></u></strong>  
Form1.cs   ************************************************
  1. using System;  
  2.   
  3. using System.Collections.Generic;  
  4.   
  5. using System.ComponentModel;  
  6.   
  7. using System.Data;  
  8.   
  9. using System.Drawing;  
  10.   
  11. using System.Text;  
  12.   
  13. using System.Windows.Forms;  
  14.   
  15. using System.IO;  
  16.   
  17.   
  18.   
  19. namespace txttest  
  20.   
  21. {  
  22.   
  23.     public partial class Form1 : Form  
  24.   
  25.     {  
  26.   
  27.         public Form1()  
  28.   
  29.         {  
  30.   
  31.             InitializeComponent();  
  32.   
  33.         }  
  34.   
  35.         /******************************************************************/  
  36.   
  37.         /******************************************************************/  
  38.   
  39.         /******************************************************************/  
  40.   
  41.         /******************************************************************/  
  42.  
  43.         #region 定义变量  
  44.   
  45.         public bool txtchange; //定义用于检查文本是否改变的变量  
  46.   
  47.         public bool save;      //定义bool类型Save判断用户选择得是SaveFile()还是SaveAsFile()  
  48.   
  49.         Search sea;       //声明查找的成员  
  50.   
  51.         Change cha;            //声明替换的成员  
  52.  
  53.         #endregion  
  54.   
  55.         #region 文件菜单  
  56.   
  57.         //新建  
  58.   
  59.         private void MnuNew_Click(object sender, EventArgs e)  
  60.   
  61.         {  
  62.   
  63.             NewFile();  
  64.   
  65.         }  
  66.   
  67.         //打开  
  68.   
  69.         private void MnuOpen_Click(object sender, EventArgs e)  
  70.   
  71.         {  
  72.   
  73.             OpenFile();  
  74.   
  75.         }  
  76.   
  77.         //保存  
  78.   
  79.         private void MnuSave_Click(object sender, EventArgs e)  
  80.   
  81.         {  
  82.   
  83.             save = true;  
  84.   
  85.             SaveFile();  
  86.   
  87.         }  
  88.   
  89.         //另存为  
  90.   
  91.         private void MnuSaveAs_Click(object sender, EventArgs e)  
  92.   
  93.         {  
  94.   
  95.             save = false;  
  96.   
  97.             SaveFile();  
  98.   
  99.         }  
  100.   
  101.         //页面设置  
  102.   
  103.         private void MnuPageSetup_Click(object sender, EventArgs e)  
  104.   
  105.         {  
  106.   
  107.             pageSetupDialog1 = new PageSetupDialog();     //初始化pageSetupDialog1的新实例  
  108.   
  109.             pageSetupDialog1.Document = printDocument;    //获取或设置一个值,指示从中获取页面设置  
  110.   
  111.             pageSetupDialog1.ShowDialog();                //弹出页面设置对话框  
  112.   
  113.         }  
  114.   
  115.         //打印预览  
  116.   
  117.         private void MnuPringPreview_Click(object sender, EventArgs e)  
  118.   
  119.         {  
  120.   
  121.             printPreviewDialog.ShowDialog();          //调用系统的打印预览对话框  
  122.   
  123.         }          
  124.   
  125.         //打印  
  126.   
  127.         private void MnuPrint_Click(object sender, EventArgs e)  
  128.   
  129.         {  
  130.   
  131.             printDocument.DocumentName = richTextBox1.Text;    //获取打印得文档名  
  132.   
  133.             printDialog.Document = printDocument;              //打印对话框  
  134.   
  135.             if (printDialog.ShowDialog() == DialogResult.OK)   //点击打印的确定按钮==OK  
  136.   
  137.             {  
  138.   
  139.                 //异常处理  
  140.   
  141.                 try   
  142.   
  143.                 {  
  144.   
  145.                     printDocument.Print();                     //try 打印时是否出错  
  146.   
  147.                 }  
  148.   
  149.                 catch(Exception ex)  
  150.   
  151.                 {  
  152.   
  153.                     MessageBox.Show(ex.Message);               //出错则弹出系统定义的错误信息  
  154.   
  155.                 }  
  156.   
  157.             }     
  158.   
  159.         }  
  160.   
  161.         //退出  
  162.   
  163.         private void MnuExit_Click(object sender, EventArgs e)  
  164.   
  165.         {  
  166.   
  167.             DialogResult res;            //实例化对话框的结果按钮为res=OK,Cancle  
  168.   
  169.             res = MessageBox.Show("确定退出记事本?""退出提示", MessageBoxButtons.OKCancel);  
  170.   
  171.             if (res == DialogResult.OK)  
  172.   
  173.             {  
  174.   
  175.                 //判断文件是否修改过   包括空格  
  176.   
  177.                 if (txtchange)  
  178.   
  179.                 {  
  180.   
  181.                     DialogResult re;     //实例化对话框的结果按钮为res=Yes,No,Cancle  
  182.   
  183.                     re = MessageBox.Show("内容已更改/n是否保存?""保存提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);  
  184.   
  185.   
  186.   
  187.                     if (re == DialogResult.Yes)  
  188.   
  189.                     {  
  190.   
  191.                         if (saveFileDialog.ShowDialog() == DialogResult.OK)//点击保存文件对话框得OK时执行下面  
  192.   
  193.                         {  
  194.   
  195.                             //获取将要保存的文件名,并将其中的文件获取并保存  
  196.   
  197.                             richTextBox1.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);  
  198.   
  199.                             Close();     //保存并退出  
  200.   
  201.                               
  202.   
  203.                         }  
  204.   
  205.                         else  
  206.   
  207.                             return;      //返回至编辑界面  
  208.   
  209.                     }  
  210.   
  211.                     else if (re == DialogResult.No)  
  212.   
  213.                     {  
  214.   
  215.                         Close();         //不保存退出  
  216.   
  217.                     }  
  218.   
  219.                     else  
  220.   
  221.                     {  
  222.   
  223.                         return;          //返回至编辑界面  
  224.   
  225.                     }  
  226.   
  227.                 }  
  228.   
  229.                 else  
  230.   
  231.                     Close();             //文件没有被修改过,直接退出  
  232.   
  233.             }  
  234.   
  235.             else  
  236.   
  237.   
  238.   
  239.                 return;                  //返回至编辑界面  
  240.   
  241.         }  
  242.   
  243.   
  244.   
  245.         //检查txtbox中内容是否有更改  
  246.   
  247.         private void richTextBox1_KeyPress_1(object sender, KeyPressEventArgs e)  
  248.   
  249.         {  
  250.   
  251.             //获取键盘的按键值  
  252.   
  253.             if (e.KeyChar.ToString() != "")  
  254.   
  255.                 //若获取到键盘按键,则txtchange=TRUE  
  256.   
  257.                 txtchange = true;  
  258.   
  259.         }  
  260.  
  261.         #endregion  
  262.   
  263.         #region 编辑菜单  
  264.   
  265.         //撤销  
  266.   
  267.         private void MnuRemove_Click(object sender, EventArgs e)  
  268.   
  269.         {  
  270.   
  271.                 richTextBox1.Undo();    //系统的撤销方法  
  272.   
  273.         }  
  274.   
  275.         //剪切  
  276.   
  277.         private void MnuCut_Click(object sender, EventArgs e)  
  278.   
  279.         {  
  280.   
  281.             richTextBox1.Cut();         //系统的剪切方法  
  282.   
  283.         }  
  284.   
  285.         //复制  
  286.   
  287.         private void MnuCopy_Click(object sender, EventArgs e)  
  288.   
  289.         {  
  290.   
  291.             richTextBox1.Copy();        //系统的复制方法  
  292.   
  293.         }  
  294.   
  295.         //粘贴  
  296.   
  297.         private void MnuStick_Click(object sender, EventArgs e)  
  298.   
  299.         {  
  300.   
  301.             richTextBox1.Paste();       //系统的粘贴方法  
  302.   
  303.         }  
  304.   
  305.         //删除  
  306.   
  307.         private void MnuDel_Click(object sender, EventArgs e)  
  308.   
  309.         {  
  310.   
  311.                 richTextBox1.SelectedText = "";   //将选定的文本设为空  
  312.   
  313.         }  
  314.   
  315.         //查找  
  316.   
  317.         private void MnuSearch_Click(object sender, EventArgs e)  
  318.   
  319.         {  
  320.   
  321.             //实例Search的成员sea  
  322.   
  323.             sea = new Search();  
  324.   
  325.             //弹出查找对话框  
  326.   
  327.             sea.Show();  
  328.   
  329.         }         
  330.   
  331.         //查找下一个  
  332.   
  333.         private void MnuSearchNext_Click(object sender, EventArgs e)  
  334.   
  335.         {  
  336.   
  337.             //实例Search的成员sea  
  338.   
  339.             sea = new Search();  
  340.   
  341.             //弹出查找对话框  
  342.   
  343.             sea.Show();  
  344.   
  345.         }  
  346.   
  347.         //替换  
  348.   
  349.         private void MnuSwap_Click(object sender, EventArgs e)  
  350.   
  351.         {  
  352.   
  353.             //实例Change的成员cha  
  354.   
  355.             cha = new Change();  
  356.   
  357.             //弹出查找下一个对话框  
  358.   
  359.             cha.Show();  
  360.   
  361.         }  
  362.   
  363.         //转到  
  364.   
  365.         private void MnuTurn_Click(object sender, EventArgs e)  
  366.   
  367.         {  
  368.   
  369.             Go go = new Go();  
  370.   
  371.             go.Show();  
  372.   
  373.         }  
  374.   
  375.         //全选  
  376.   
  377.         private void MnuFull_Click(object sender, EventArgs e)  
  378.   
  379.         {  
  380.   
  381.             richTextBox1.SelectAll();      //系统全选的方法  
  382.   
  383.         }  
  384.   
  385.         //时间 -- 日期  
  386.   
  387.         private void MnuTime_Click(object sender, EventArgs e)  
  388.   
  389.         {  
  390.   
  391.             richTextBox1.Text += DateTime.Now.ToString();   //添加系统当前时间  
  392.   
  393.         }  
  394.  
  395.  
  396.  
  397.         #endregion  
  398.  
  399.  
  400.  
  401.         #region 格式菜单  
  402.   
  403.         //自动换行  
  404.   
  405.         private void MnuLineWrap_Click(object sender, EventArgs e)  
  406.   
  407.         {  
  408.   
  409.             if (richTextBox1.WordWrap)         //WordWrap为richTextBox的换行属性  
  410.   
  411.             {  
  412.   
  413.                 richTextBox1.WordWrap = false//true时为自动换行  
  414.   
  415.             }  
  416.   
  417.             else  
  418.   
  419.                 richTextBox1.WordWrap = true;     
  420.   
  421.         }  
  422.   
  423.         //字体  
  424.   
  425.         private void MnuFont_Click(object sender, EventArgs e)  
  426.   
  427.         {  
  428.   
  429.             FontDialog ftDg = new FontDialog(); //实例化字体设置  
  430.   
  431.             ftDg.ShowColor = true;              //设置字体颜色  
  432.   
  433.             ftDg.AllowScriptChange = true;      //获取外部字符集  
  434.   
  435.             ftDg.AllowVectorFonts = true;       //斜体字  
  436.   
  437.             ftDg.ShowEffects = true;            //删除线,下划线,字体颜色  
  438.   
  439.   
  440.   
  441.             if (ftDg.ShowDialog() == DialogResult.OK)  
  442.   
  443.             {  
  444.   
  445.                 richTextBox1.Font = ftDg.Font;  
  446.   
  447.                 richTextBox1.ForeColor = ftDg.Color;  
  448.   
  449.             }  
  450.   
  451.         }  
  452.  
  453.         #endregion  
  454.   
  455.         #region 查看菜单  
  456.   
  457.         //状态栏  
  458.   
  459.         private void MnuStatusBar_Click(object sender, EventArgs e)  
  460.   
  461.         {  
  462.   
  463.             if (statusStrip1.Visible)           //状态栏的属性Visible  
  464.   
  465.             {  
  466.   
  467.                 statusStrip1.Visible = false;   //true时为显示状态栏  
  468.   
  469.             }  
  470.   
  471.             else  
  472.   
  473.             {  
  474.   
  475.                 statusStrip1.Visible = true;                  
  476.   
  477.             }  
  478.   
  479.         }  
  480.  
  481.         #endregion  
  482.   
  483.         #region 帮助菜单  
  484.   
  485.         //帮助主题  
  486.   
  487.         private void MnuTheme_Click(object sender, EventArgs e)  
  488.   
  489.         {  
  490.   
  491.             System.Diagnostics.Process.Start(@"c:/windows/help/notepad.chm");    //调出系统目录下得记事本帮助文档  
  492.   
  493.         }  
  494.   
  495.         //关于记事本  
  496.   
  497.         private void MnuAbout_Click(object sender, EventArgs e)  
  498.   
  499.         {  
  500.   
  501.             //实例化About类成员ab  
  502.   
  503.             About ab = new About();  
  504.   
  505.             //为成员控件赋值  
  506.   
  507.             ab.label5.Text = "制作:/n/n汪祖华-李涛-孙远飞-闫兴/n/n技术支持:鄢涛老师";  
  508.   
  509.             //调出成员对话框  
  510.   
  511.             ab.Show();  
  512.   
  513.         }  
  514.  
  515.         #endregion  
  516.  
  517.  
  518.  
  519.         #region 被调用的方法  
  520.   
  521.         //新建文件的方法  
  522.   
  523.         private void NewFile()  
  524.   
  525.         {  
  526.   
  527.             //判断文件是否修改过   包括空格  
  528.   
  529.             if (txtchange)  
  530.   
  531.             {  
  532.   
  533.                 //实例化对话框的成员re为对话框的按钮Yes,No,Cancle  
  534.   
  535.                 DialogResult re;  
  536.   
  537.                 re = MessageBox.Show("内容已更改,是否保存?""保存提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);  
  538.   
  539.                 //判断所选按钮  
  540.   
  541.                 switch (re)  
  542.   
  543.                 {  
  544.   
  545.                     case DialogResult.Yes:            //若选择yes弹出保存对话框  
  546.   
  547.                         {  
  548.   
  549.                             //若选择保存按钮执行下列  
  550.   
  551.                             if (saveFileDialog.ShowDialog() == DialogResult.OK)  
  552.   
  553.                             {  
  554.   
  555.                                 //获取将要保存的文件名,并将其中的文件获取并保存  
  556.   
  557.                                 richTextBox1.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);  
  558.   
  559.                                 //保存后清空内容  ,相当于新建文件  
  560.   
  561.                                 richTextBox1.Clear();  
  562.   
  563.                             }  
  564.   
  565.                             else  
  566.   
  567.                                 return;          //不保存,返回文件编辑界面  
  568.   
  569.                         }  
  570.   
  571.                         break;                   //switch,case的结构是每一个case都要有break  
  572.   
  573.                     case DialogResult.No:        //选择不保存文件  
  574.   
  575.                         richTextBox1.Clear();    //清空内容  ,相当于新建文件  
  576.   
  577.                         break;  
  578.   
  579.                     case DialogResult.Cancel:    //选择取消,  
  580.   
  581.                         break;                   //不对文件内容做更改,直接返回  
  582.   
  583.                 }  
  584.   
  585.             }  
  586.   
  587.             else  
  588.   
  589.                 richTextBox1.Clear();             //文件内容未作更改,直接清空文件内容  
  590.   
  591.         }  
  592.   
  593.         //打开文件的方法  
  594.   
  595.         private void OpenFile()  
  596.   
  597.         {  
  598.   
  599.             //判断文件是否修改过   包括空格  
  600.   
  601.             if (txtchange)  
  602.   
  603.             {  
  604.   
  605.                 //实例化对话框的成员re为对话框的按钮Yes,No,Cancle  
  606.   
  607.                 DialogResult re;  
  608.   
  609.                 re = MessageBox.Show("内容已更改,是否保存?""保存提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);  
  610.   
  611.                 //判断所选按钮  
  612.   
  613.                 switch (re)  
  614.   
  615.                 {  
  616.   
  617.                     case DialogResult.Yes:        //选择保存  
  618.   
  619.                         {  
  620.   
  621.   
  622.   
  623.                             //如果保存对话框选择保存执行以下  
  624.   
  625.                             if (saveFileDialog.ShowDialog() == DialogResult.OK)  
  626.   
  627.                             {  
  628.   
  629.                                 //获取将要保存的文件名,并将其中的文件获取并保存  
  630.   
  631.                                 richTextBox1.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);  
  632.   
  633.                                 //保存完毕,调用打开文件方法                                  
  634.   
  635.                                 OpenFile_2();  
  636.   
  637.                             }  
  638.   
  639.                         }  
  640.   
  641.                         break;     //返回新打开的文件  
  642.   
  643.                     case DialogResult.No:       //选择不保存,直接弹出打开文件对话框  
  644.   
  645.                         {                              
  646.   
  647.                             OpenFile_2();  
  648.   
  649.                         }  
  650.   
  651.                         break;           //返回新打开的文件  
  652.   
  653.                     case DialogResult.Cancel:     //选择取消按钮,返回至编辑界面  
  654.   
  655.                         break;  
  656.   
  657.                 }  
  658.   
  659.             }  
  660.   
  661.             else  
  662.   
  663.             {  
  664.   
  665.                 OpenFile_2();        //文件内容没有更改,直接弹出打开文件对话框  
  666.   
  667.             }          
  668.   
  669.         }  
  670.   
  671.         //保存和另存的方法  
  672.   
  673.         private void SaveFile()  
  674.   
  675.         {  
  676.   
  677.             //当用户单击保存时save = true  
  678.   
  679.             if (save == true)  
  680.   
  681.             {  
  682.   
  683.                 //选择保存对话框按钮 OK  
  684.   
  685.                 if (saveFileDialog.ShowDialog() == DialogResult.OK)  
  686.   
  687.                 {  
  688.   
  689.                     //获取将要保存的文件名,并将其中的文件获取并保存  
  690.   
  691.                     richTextBox1.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);  
  692.   
  693.                 }  
  694.   
  695.                 else  
  696.   
  697.                     return;     //返回至编辑界面  
  698.   
  699.             }  
  700.   
  701.             else    //此为用户选择另存为按钮时save = false  
  702.   
  703.             {  
  704.   
  705.                 //选择另存对话框按钮 OK  
  706.   
  707.                 if (saveAsFileDialog.ShowDialog() == DialogResult.OK)  
  708.   
  709.                 {  
  710.   
  711.                     //获取将要保存的文件名,并将其中的文件获取并保存  
  712.   
  713.                     richTextBox1.SaveFile(saveAsFileDialog.FileName, RichTextBoxStreamType.PlainText);  
  714.   
  715.                 }  
  716.   
  717.                 else  
  718.   
  719.                     return;      //返回至编辑界面  
  720.   
  721.             }  
  722.   
  723.         }  
  724.   
  725.         private void OpenFile_2()  
  726.   
  727.         {  
  728.   
  729.             //弹出打开文件对话框  
  730.   
  731.             if (openFileDialog.ShowDialog() == DialogResult.OK)  
  732.   
  733.             {  
  734.   
  735.                 //将Form1的Text属性赋值为文件名  
  736.   
  737.                 Text = openFileDialog.FileName;  
  738.   
  739.                 //获取文件内容,加载数据流至richTextBox.Text  
  740.   
  741.                 richTextBox1.LoadFile(openFileDialog.FileName, RichTextBoxStreamType.PlainText);  
  742.   
  743.             }  
  744.   
  745.         }  
  746.  
  747.         #endregion  
  748.  
  749.  
  750.  
  751.         #region 其他事件  
  752.   
  753.         //鼠标位置  
  754.   
  755.         private void richTextBox1_MouseMove_1(object sender, MouseEventArgs e)  
  756.   
  757.         {  
  758.   
  759.             //将鼠标的坐标显示在任务栏的toolLabel_Mouse的属性Text中  
  760.   
  761.             toolLabel_Mouse.Text = e.X.ToString() + "." + e.Y.ToString();  
  762.   
  763.         }  
  764.   
  765.         //显示系统时间          
  766.   
  767.         private void timer1_Tick(object sender, EventArgs e)  
  768.   
  769.         {  
  770.   
  771.             //将系统时间赋给任务栏的toolLabel_Time的属性Text中  
  772.   
  773.             toolLabel_Time.Text = DateTime.Now.ToString();  
  774.   
  775.               
  776.   
  777.         }  
  778.   
  779.         //打开时的时间  
  780.   
  781.         private void Form1_Load(object sender, EventArgs e)  
  782.   
  783.         {  
  784.   
  785.             //将加载窗体时的时间赋给toolLabel_Opentime.Text  
  786.   
  787.             toolLabel_Opentime.Text = DateTime.Now.ToString("MM-dd H:mm:ss");  //MM-dd H:mm:ss   其中得一个H表示上午时显示一位数,例如8点,而不是显示08  
  788.   
  789.         }  
  790.  
  791.         #endregion  
  792.   
  793.         #region richtextbox     右键菜单  
  794.   
  795.         //*****************右键菜单实现的功能与编辑菜单中相应的编辑键有相同的功能***********//  
  796.   
  797.         private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)  
  798.   
  799.         {  
  800.   
  801.             richTextBox1.Undo();  
  802.   
  803.         }  
  804.   
  805.   
  806.   
  807.         private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)  
  808.   
  809.         {  
  810.   
  811.             richTextBox1.Copy();  
  812.   
  813.         }  
  814.   
  815.   
  816.   
  817.         private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)  
  818.   
  819.         {  
  820.   
  821.             richTextBox1.Cut();  
  822.   
  823.         }  
  824.   
  825.   
  826.   
  827.         private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)  
  828.   
  829.         {  
  830.   
  831.             richTextBox1.Paste();  
  832.   
  833.         }  
  834.   
  835.   
  836.   
  837.         private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)  
  838.   
  839.         {  
  840.   
  841.             richTextBox1.SelectedText = "";  
  842.   
  843.         }  
  844.  
  845.         #endregion  
  846.   
  847.         #region 菜单            右键菜单  
  848.   
  849.         //当鼠标再菜单和任务栏右击时的菜单  
  850.   
  851.         private void 关于记事本ToolStripMenuItem_Click(object sender, EventArgs e)  
  852.   
  853.         {  
  854.   
  855.             About ab = new About();  
  856.   
  857.             ab.label5.Text = "制作:/n/n汪祖华-李涛-孙远飞-闫兴/n/n技术支持:鄢涛老师";  
  858.   
  859.             ab.Show();  
  860.   
  861.         }  
  862.  
  863.         #endregion  
  864.   
  865.     }  
  866.   
  867. }  
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;



namespace txttest

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        /******************************************************************/

        /******************************************************************/

        /******************************************************************/

        /******************************************************************/

        #region 定义变量

        public bool txtchange; //定义用于检查文本是否改变的变量

        public bool save;      //定义bool类型Save判断用户选择得是SaveFile()还是SaveAsFile()

        Search sea;       //声明查找的成员

        Change cha;            //声明替换的成员

        #endregion

        #region 文件菜单

        //新建

        private void MnuNew_Click(object sender, EventArgs e)

        {

            NewFile();

        }

        //打开

        private void MnuOpen_Click(object sender, EventArgs e)

        {

            OpenFile();

        }

        //保存

        private void MnuSave_Click(object sender, EventArgs e)

        {

            save = true;

            SaveFile();

        }

        //另存为

        private void MnuSaveAs_Click(object sender, EventArgs e)

        {

            save = false;

            SaveFile();

        }

        //页面设置

        private void MnuPageSetup_Click(object sender, EventArgs e)

        {

            pageSetupDialog1 = new PageSetupDialog();     //初始化pageSetupDialog1的新实例

            pageSetupDialog1.Document = printDocument;    //获取或设置一个值,指示从中获取页面设置

            pageSetupDialog1.ShowDialog();                //弹出页面设置对话框

        }

        //打印预览

        private void MnuPringPreview_Click(object sender, EventArgs e)

        {

            printPreviewDialog.ShowDialog();          //调用系统的打印预览对话框

        }        

        //打印

        private void MnuPrint_Click(object sender, EventArgs e)

        {

            printDocument.DocumentName = richTextBox1.Text;    //获取打印得文档名

            printDialog.Document = printDocument;              //打印对话框

            if (printDialog.ShowDialog() == DialogResult.OK)   //点击打印的确定按钮==OK

            {

                //异常处理

                try 

                {

                    printDocument.Print();                     //try 打印时是否出错

                }

                catch(Exception ex)

                {

                    MessageBox.Show(ex.Message);               //出错则弹出系统定义的错误信息

                }

            }   

        }

        //退出

        private void MnuExit_Click(object sender, EventArgs e)

        {

            DialogResult res;            //实例化对话框的结果按钮为res=OK,Cancle

            res = MessageBox.Show("确定退出记事本?", "退出提示", MessageBoxButtons.OKCancel);

            if (res == DialogResult.OK)

            {

                //判断文件是否修改过   包括空格

                if (txtchange)

                {

                    DialogResult re;     //实例化对话框的结果按钮为res=Yes,No,Cancle

                    re = MessageBox.Show("内容已更改/n是否保存?", "保存提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);



                    if (re == DialogResult.Yes)

                    {

                        if (saveFileDialog.ShowDialog() == DialogResult.OK)//点击保存文件对话框得OK时执行下面

                        {

                            //获取将要保存的文件名,并将其中的文件获取并保存

                            richTextBox1.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);

                            Close();     //保存并退出

                            

                        }

                        else

                            return;      //返回至编辑界面

                    }

                    else if (re == DialogResult.No)

                    {

                        Close();         //不保存退出

                    }

                    else

                    {

                        return;          //返回至编辑界面

                    }

                }

                else

                    Close();             //文件没有被修改过,直接退出

            }

            else



                return;                  //返回至编辑界面

        }



        //检查txtbox中内容是否有更改

        private void richTextBox1_KeyPress_1(object sender, KeyPressEventArgs e)

        {

            //获取键盘的按键值

            if (e.KeyChar.ToString() != "")

                //若获取到键盘按键,则txtchange=TRUE

                txtchange = true;

        }

        #endregion

        #region 编辑菜单

        //撤销

        private void MnuRemove_Click(object sender, EventArgs e)

        {

                richTextBox1.Undo();    //系统的撤销方法

        }

        //剪切

        private void MnuCut_Click(object sender, EventArgs e)

        {

            richTextBox1.Cut();         //系统的剪切方法

        }

        //复制

        private void MnuCopy_Click(object sender, EventArgs e)

        {

            richTextBox1.Copy();        //系统的复制方法

        }

        //粘贴

        private void MnuStick_Click(object sender, EventArgs e)

        {

            richTextBox1.Paste();       //系统的粘贴方法

        }

        //删除

        private void MnuDel_Click(object sender, EventArgs e)

        {

                richTextBox1.SelectedText = "";   //将选定的文本设为空

        }

        //查找

        private void MnuSearch_Click(object sender, EventArgs e)

        {

            //实例Search的成员sea

            sea = new Search();

            //弹出查找对话框

            sea.Show();

        }       

        //查找下一个

        private void MnuSearchNext_Click(object sender, EventArgs e)

        {

            //实例Search的成员sea

            sea = new Search();

            //弹出查找对话框

            sea.Show();

        }

        //替换

        private void MnuSwap_Click(object sender, EventArgs e)

        {

            //实例Change的成员cha

            cha = new Change();

            //弹出查找下一个对话框

            cha.Show();

        }

        //转到

        private void MnuTurn_Click(object sender, EventArgs e)

        {

            Go go = new Go();

            go.Show();

        }

        //全选

        private void MnuFull_Click(object sender, EventArgs e)

        {

            richTextBox1.SelectAll();      //系统全选的方法

        }

        //时间 -- 日期

        private void MnuTime_Click(object sender, EventArgs e)

        {

            richTextBox1.Text += DateTime.Now.ToString();   //添加系统当前时间

        }



        #endregion



        #region 格式菜单

        //自动换行

        private void MnuLineWrap_Click(object sender, EventArgs e)

        {

            if (richTextBox1.WordWrap)         //WordWrap为richTextBox的换行属性

            {

                richTextBox1.WordWrap = false; //true时为自动换行

            }

            else

                richTextBox1.WordWrap = true;   

        }

        //字体

        private void MnuFont_Click(object sender, EventArgs e)

        {

            FontDialog ftDg = new FontDialog(); //实例化字体设置

            ftDg.ShowColor = true;              //设置字体颜色

            ftDg.AllowScriptChange = true;      //获取外部字符集

            ftDg.AllowVectorFonts = true;       //斜体字

            ftDg.ShowEffects = true;            //删除线,下划线,字体颜色



            if (ftDg.ShowDialog() == DialogResult.OK)

            {

                richTextBox1.Font = ftDg.Font;

                richTextBox1.ForeColor = ftDg.Color;

            }

        }

        #endregion

        #region 查看菜单

        //状态栏

        private void MnuStatusBar_Click(object sender, EventArgs e)

        {

            if (statusStrip1.Visible)           //状态栏的属性Visible

            {

                statusStrip1.Visible = false;   //true时为显示状态栏

            }

            else

            {

                statusStrip1.Visible = true;                

            }

        }

        #endregion

        #region 帮助菜单

        //帮助主题

        private void MnuTheme_Click(object sender, EventArgs e)

        {

            System.Diagnostics.Process.Start(@"c:/windows/help/notepad.chm");    //调出系统目录下得记事本帮助文档

        }

        //关于记事本

        private void MnuAbout_Click(object sender, EventArgs e)

        {

            //实例化About类成员ab

            About ab = new About();

            //为成员控件赋值

            ab.label5.Text = "制作:/n/n汪祖华-李涛-孙远飞-闫兴/n/n技术支持:鄢涛老师";

            //调出成员对话框

            ab.Show();

        }

        #endregion



        #region 被调用的方法

        //新建文件的方法

        private void NewFile()

        {

            //判断文件是否修改过   包括空格

            if (txtchange)

            {

                //实例化对话框的成员re为对话框的按钮Yes,No,Cancle

                DialogResult re;

                re = MessageBox.Show("内容已更改,是否保存?", "保存提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                //判断所选按钮

                switch (re)

                {

                    case DialogResult.Yes:            //若选择yes弹出保存对话框

                        {

                            //若选择保存按钮执行下列

                            if (saveFileDialog.ShowDialog() == DialogResult.OK)

                            {

                                //获取将要保存的文件名,并将其中的文件获取并保存

                                richTextBox1.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);

                                //保存后清空内容  ,相当于新建文件

                                richTextBox1.Clear();

                            }

                            else

                                return;          //不保存,返回文件编辑界面

                        }

                        break;                   //switch,case的结构是每一个case都要有break

                    case DialogResult.No:        //选择不保存文件

                        richTextBox1.Clear();    //清空内容  ,相当于新建文件

                        break;

                    case DialogResult.Cancel:    //选择取消,

                        break;                   //不对文件内容做更改,直接返回

                }

            }

            else

                richTextBox1.Clear();             //文件内容未作更改,直接清空文件内容

        }

        //打开文件的方法

        private void OpenFile()

        {

            //判断文件是否修改过   包括空格

            if (txtchange)

            {

                //实例化对话框的成员re为对话框的按钮Yes,No,Cancle

                DialogResult re;

                re = MessageBox.Show("内容已更改,是否保存?", "保存提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                //判断所选按钮

                switch (re)

                {

                    case DialogResult.Yes:        //选择保存

                        {



                            //如果保存对话框选择保存执行以下

                            if (saveFileDialog.ShowDialog() == DialogResult.OK)

                            {

                                //获取将要保存的文件名,并将其中的文件获取并保存

                                richTextBox1.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);

                                //保存完毕,调用打开文件方法                                

                                OpenFile_2();

                            }

                        }

                        break;     //返回新打开的文件

                    case DialogResult.No:       //选择不保存,直接弹出打开文件对话框

                        {                            

                            OpenFile_2();

                        }

                        break;           //返回新打开的文件

                    case DialogResult.Cancel:     //选择取消按钮,返回至编辑界面

                        break;

                }

            }

            else

            {

                OpenFile_2();        //文件内容没有更改,直接弹出打开文件对话框

            }        

        }

        //保存和另存的方法

        private void SaveFile()

        {

            //当用户单击保存时save = true

            if (save == true)

            {

                //选择保存对话框按钮 OK

                if (saveFileDialog.ShowDialog() == DialogResult.OK)

                {

                    //获取将要保存的文件名,并将其中的文件获取并保存

                    richTextBox1.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);

                }

                else

                    return;     //返回至编辑界面

            }

            else    //此为用户选择另存为按钮时save = false

            {

                //选择另存对话框按钮 OK

                if (saveAsFileDialog.ShowDialog() == DialogResult.OK)

                {

                    //获取将要保存的文件名,并将其中的文件获取并保存

                    richTextBox1.SaveFile(saveAsFileDialog.FileName, RichTextBoxStreamType.PlainText);

                }

                else

                    return;      //返回至编辑界面

            }

        }

        private void OpenFile_2()

        {

            //弹出打开文件对话框

            if (openFileDialog.ShowDialog() == DialogResult.OK)

            {

                //将Form1的Text属性赋值为文件名

                Text = openFileDialog.FileName;

                //获取文件内容,加载数据流至richTextBox.Text

                richTextBox1.LoadFile(openFileDialog.FileName, RichTextBoxStreamType.PlainText);

            }

        }

        #endregion



        #region 其他事件

        //鼠标位置

        private void richTextBox1_MouseMove_1(object sender, MouseEventArgs e)

        {

            //将鼠标的坐标显示在任务栏的toolLabel_Mouse的属性Text中

            toolLabel_Mouse.Text = e.X.ToString() + "." + e.Y.ToString();

        }

        //显示系统时间        

        private void timer1_Tick(object sender, EventArgs e)

        {

            //将系统时间赋给任务栏的toolLabel_Time的属性Text中

            toolLabel_Time.Text = DateTime.Now.ToString();

            

        }

        //打开时的时间

        private void Form1_Load(object sender, EventArgs e)

        {

            //将加载窗体时的时间赋给toolLabel_Opentime.Text

            toolLabel_Opentime.Text = DateTime.Now.ToString("MM-dd H:mm:ss");  //MM-dd H:mm:ss   其中得一个H表示上午时显示一位数,例如8点,而不是显示08

        }

        #endregion

        #region richtextbox     右键菜单

        //*****************右键菜单实现的功能与编辑菜单中相应的编辑键有相同的功能***********//

        private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            richTextBox1.Undo();

        }



        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            richTextBox1.Copy();

        }



        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            richTextBox1.Cut();

        }



        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            richTextBox1.Paste();

        }



        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            richTextBox1.SelectedText = "";

        }

        #endregion

        #region 菜单            右键菜单

        //当鼠标再菜单和任务栏右击时的菜单

        private void 关于记事本ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            About ab = new About();

            ab.label5.Text = "制作:/n/n汪祖华-李涛-孙远飞-闫兴/n/n技术支持:鄢涛老师";

            ab.Show();

        }

        #endregion

    }

}
  1. <font size="4" color="#ff0000"><strong><u>Class1.cs   </u></strong></font><strong><u><font size="4" color="#ff0000">************************************************</font></u></strong>  
Class1.cs   ************************************************
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Windows.Forms;  
  5. //using System.IO;  
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
//using System.IO;
  1. namespace txttest  
  2. {  
  3.     class Class1  
  4.     {  
namespace txttest
{
    class Class1
    {
  1.         public void About_1()  
  2.         {  
  3.             System.Diagnostics.Process.Start("<a href="http://www.cdu.edu.cn/">http://www.cdu.edu.cn/</a>");  
  4.         }  
  5.         public void About_2()  
  6.         {  
  7.             System.Diagnostics.Process.Start("<a href="http://computer.cdu.edu.cn/">http://computer.cdu.edu.cn/</a>");  
  8.         }  
  9.         public void About_3()  
  10.         {  
  11.             System.Diagnostics.Process.Start("<a href="http://computer.cdu.edu.cn/teacher/teacherIndex.action?id=4af3db8e1902c49a01190362138f003b">http://computer.cdu.edu.cn/teacher/teacherIndex.action?id=4af3db8e1902c49a01190362138f003b</a>");          
  12.         }  
  13.     }  
  14. }  
        public void About_1()
        {
            System.Diagnostics.Process.Start("http://www.cdu.edu.cn/");
        }
        public void About_2()
        {
            System.Diagnostics.Process.Start("http://computer.cdu.edu.cn/");
        }
        public void About_3()
        {
            System.Diagnostics.Process.Start("http://computer.cdu.edu.cn/teacher/teacherIndex.action?id=4af3db8e1902c49a01190362138f003b");        
        }
    }
}
  1. <font size="4" color="#ff0000"><strong><u>Class2.cs   </u></strong></font><strong><u><font size="4" color="#ff0000">*************************************************</font></u></strong>  
Class2.cs   *************************************************
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
using System;
using System.Collections.Generic;
using System.Text;
  1. namespace txttest  
  2. {  
  3.     class Class2 : Class1  
  4.     {  
  5.         //继承于:Class1  
  6.     }  
  7. }  
namespace txttest
{
    class Class2 : Class1
    {
        //继承于:Class1
    }
}
  1. <font size="4" color="#ff0000"><strong><u>About.cs   **************************************************</u></strong></font>  
About.cs   **************************************************
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
  1. namespace txttest  
  2. {  
  3.     public partial class About : Form  
  4.     {  
  5.         public About()  
  6.         {  
  7.             InitializeComponent();  
  8.         }  
  9.         /******************************************************************/  
  10.         /******************************************************************/  
  11.         /******************************************************************/  
  12.         /******************************************************************/  
  13.         Class2 sp = new Class2();  
  14.         private void timer1_Tick(object sender, EventArgs e)  
  15.         {  
  16.             label4.Text = DateTime.Now.ToLongDateString() + "/n" + DateTime.Now.ToLongTimeString();  
namespace txttest
{
    public partial class About : Form
    {
        public About()
        {
            InitializeComponent();
        }
        /******************************************************************/
        /******************************************************************/
        /******************************************************************/
        /******************************************************************/
        Class2 sp = new Class2();
        private void timer1_Tick(object sender, EventArgs e)
        {
            label4.Text = DateTime.Now.ToLongDateString() + "/n" + DateTime.Now.ToLongTimeString();
  1.         }  
        }
  1.         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)  
  2.         {  
  3.             sp.About_1();  
  4.         }  
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            sp.About_1();
        }
  1.         private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)  
  2.         {  
  3.             sp.About_2();  
  4.         }  
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            sp.About_2();
        }
  1.         private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)  
  2.         {  
  3.             sp.About_3();  
  4.         }  
  5.     }  

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 Mickey记事本 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // 用于存储当前操作的文件的名称 private string textFileName = ""; private string filePath = ""; private void 新建_Click(object sender, EventArgs e) { textFileName = ""; // 新建一个文本时,若输入框中的内容不为空,应先提示“是否保存” if (inputInfo.Text != string.Empty) { // 若用户选择“是”,应弹出保存文件的对话框 if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) == DialogResult.Yes) { // 保存文件 Save(); Text = "新建-Mickey记事本"; inputInfo.Text = ""; } else if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) == DialogResult.No) { // 用户选择不保存时将输入框中的内容清除 inputInfo.Text = ""; } } } private void 打开_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "文本文件(*.txt)|*.txt"; if (openFile.ShowDialog() == DialogResult.OK) { StreamReader sr = new StreamReader(openFile.FileName); inputInfo.Text = sr.ReadToEnd(); sr.Close(); FileInfo fileInfo = new FileInfo(openFile.FileName); // 把标题改为打开的文件的名称 Text = "*" + fileInfo.Name + "-Mickey记事本"; textFileName = fileInfo.Name; } } private void 保存_Click(object sender, EventArgs e) { Save(); } // “保存” private void Save() { if (!textFileName.Equals("")) { SaveFileDialog saveFile = new SaveFileDialog(); // 默认保存格式 saveFile.Filter = "文本文件(*.txt)|*.txt"; StreamWriter sw = new StreamWriter(filePath, false); sw.Write(inputInfo.Text); sw.Close(); MessageBox.Show("文件保存成功!", "Mickey温馨提示"); filePath = saveFile.FileName; // 把标题改为打开的文件的名称 Text = textFileName + "-Mickey记事本"; } else { // 成员变量为“”,说明文件第一次保存,执行“另存为”操作 HoldFile(); } } private void HoldFile() { // 若用户选择另保存文件 SaveFileDialog saveFile = new SaveFileDialog(); // 默认保存格式 saveFile.Filter = "文本文件(*.txt)|*.txt"; if (saveFile.ShowDialog() == DialogResult.OK) { StreamWriter sw = new StreamWriter(saveFile.FileName, false); sw.WriteLine(inputInfo.Text); sw.Close(); MessageBox.Show("文件保存成功!", "Mickey温馨提示"); filePath = saveFile.FileName; } // 判断是第一次保存还是第二次 if (textFileName.Equals("")) { FileInfo fileInfo = new FileInfo(saveFile.FileName); Text = fileInfo.Name + "-Mickey记事本"; textFileName = fileInfo.Name; } else { // 把标题改为打开的文件的名称 Text = textFileName + "-Mickey记事本"; } } private void 另存为_Click(object sender, EventArgs e) { HoldFile(); } private void 页面设置_Click(object sender, EventArgs e) { this.pageSetupDialog1.Document = this.printDocument1; pageSetupDialog1.ShowDialog(); } private void 打印_Click(object sender, EventArgs e) { if (inputInfo.Text.Length 0) { 状态栏.Enabled = true; } else { 状态栏.Enabled = false; } } private void 状态栏_Click(object sender, EventArgs e) { if (状态栏.Checked == true) { 状态栏.Checked = false; statusStrip1.Visible = false; } else { 状态栏.Checked = true; statusStrip1.Visible = true; } } private void 查看帮助_Click(object sender, EventArgs e) { string help = @"C:\Users\狗狗Mickey\Desktop\help.txt"; Help.ShowHelp(this, help); } private void 关于记事本_Click(object sender, EventArgs e) { AboutBox1 about = new AboutBox1(); about.Show(); } } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值