C#窗体实例:记事本

用VS写记事本其实很简单,现成的控件现成的代码,一边打开一个电脑上的记事本,一边照着写菜单,然后编辑功能。VS里自带很多控件,能够直接使用来完成所要的功能。

运行效果如图:




在窗体中添加menuStrip控件来书写菜单:




在整个记事本软件中将会用到下列控件,都是一些比较常用的控件,就不用多说,主要写一下部分功能的代码:



1.新建

   private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            if (richTextBox1.Text != "")
            {

                if (bIsSave == false) //没有保存
                {
                    DialogResult result = MessageBox.Show("文件 无标题 的文字已改变。\n\n想保存文件吗?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

                    if (result == DialogResult.Yes)
                    {
                        saveFileDialog1.FileName = "*.txt";
                        saveFileDialog1.Filter = "文本文档(*.txt)|*.txt";
                        saveFileDialog1.Title = "保存";
                        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                            filename = saveFileDialog1.FileName;
                            this.Text = "无标题 - 记事本";
                            richTextBox1.Clear();
                        }
                    }
                    if (result == DialogResult.No)
                    {
                        richTextBox1.Clear();
                    }
                }
                else 
                {
                    richTextBox1.Clear();
                }
            }
        }

2.复制

  private void 复制CCtrlCToolStripMenuItem_Click(object sender, EventArgs e)
        {
              // richTextBox1.Copy();
                 this.Cursor = Cursors.WaitCursor;
                 string strTemp = richTextBox1.SelectedText;   //获取RichTextBox中选中的文字 
                 if (strTemp.Equals(""))     //Equals 方法确定两个对象是否相等
                 return;  
                 Clipboard.Clear();//清除原有剪切板中内容  
                 Clipboard.SetText(strTemp);//将文字添加到剪切板中,还添加Object类型数据                
                 this.Cursor = Cursors.Default;      
        }


3.打开

  private void 打开OCtrlOToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "*.txt";
            openFileDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.Default);
                richTextBox1.Text = sr.ReadToEnd();
                sr.Close();
            }
            filename = openFileDialog1.FileName;
            this.Text = Path.GetFileName(openFileDialog1.FileName)+" - 记事本";
            bIsSave = true;
        }

4.保存

 private void 保存SCtrlSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (filename == "")
            {
                bIsSave = false;
                saveFileDialog1.FileName = "*.txt";
                saveFileDialog1.Filter = "文本文档(*.txt)|*.txt";
                saveFileDialog1.Title = "保存";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                    filename = saveFileDialog1.FileName;
                    this.Text = "无标题 - 记事本";
                    richTextBox1.Clear();
                }
                else
                {
                    richTextBox1.SaveFile(filename , RichTextBoxStreamType.PlainText);
                }
            }
            bIsSave = true;
        }

5.另存为

 private void 另存为ACtrlAToolStripMenuItem_Click(object sender, EventArgs e)
        {
                saveFileDialog1.FileName = "*.txt";
                saveFileDialog1.Filter = "文本文档(*.txt)|*.txt";
                saveFileDialog1.Title = "保存";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                    filename = saveFileDialog1.FileName;
                    this.Text = "无标题 - 记事本";
                    richTextBox1.Clear();
                }
                bIsSave = true ;          
        }

6.退出

 private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text != "")
            {
                if (bIsSave == false)
                {
                    DialogResult result = MessageBox.Show("文件 无标题 的文字已改变。\n\n想保存文件吗?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                    if (result == DialogResult.Yes)
                    {
                        保存SCtrlSToolStripMenuItem_Click(sender, e);
                    }
                    else if (result == DialogResult.No)
                    {
                        this.Close();
                    }
                    else if (result == DialogResult.Cancel)
                    {
                    }
                }
            }
            else
                this.Close();
            //Application.Exit();
        }

7.打印

 private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            printDialog1.Document = printDocument1;
            printDialog1.ShowDialog();
        }

8.页面设计

 //页面设计
        private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pageSetupDialog1.Document = printDocument1;
            pageSetupDialog1.ShowDialog();
        }

9.剪切

  private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
                 this.Cursor = Cursors.WaitCursor;
                 string strTemp = richTextBox1.SelectedText;
                 if (strTemp.Equals(""))  
                     return;  
                 Clipboard.Clear();  
                 richTextBox1.Cut();  
                 this.Cursor = Cursors.Default;                    
        }

10.撤销

  private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
               this.Cursor = Cursors.WaitCursor;  
               richTextBox1.Undo();  
               this.Cursor = Cursors.Default;             
        }

11.删除

     private void 删除LDelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Undo();
        }

12.自动换行

  private void 自动换行WToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (自动换行WToolStripMenuItem.Checked)
            {
                自动换行WToolStripMenuItem.Checked = false;
                richTextBox1.WordWrap = false ;
            }
            else
            {
                自动换行WToolStripMenuItem.Checked = true;
                richTextBox1.WordWrap = true ;
            }
        }

13.字体

   private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowDialog();
        }

14.全选

   private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Focus();//设置定位到当前活动的RichTextBox,这一句很重要,否则它不能正确执行 
            richTextBox1.SelectAll();
        }

15.时间日期

  private void 时间日期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //System.DateTime currentTime = new System.DateTime();
            string Time;
            Time = (System.DateTime.Now).ToString ();
            richTextBox1.Text = Time;
        }

16.查找

 private void 查找FCtrlFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Find frmFind = new Find(richTextBox1);
            frmFind.Show(this);
        }

17.查找下一个

private void 查找下一个ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Find frmFind = new Find(richTextBox1);
            frmFind.Show(this);
        }

18.替换

  private void 替换RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Replace frmReplace = new Replace(richTextBox1 );
            frmReplace.Show(this);
        }

19.转到

  private void 转到GToolStripMenuItem_Click(object sender, EventArgs e)
        {
            zhuan frmzhuan = new zhuan(richTextBox1);
            frmzhuan.Show(this);
        }










©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页