c#记事本代码

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;

namespace note
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool b=false, s=true;
        private void 字体VToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (b == true || richTextBox1.Text.Trim() != " ")
            {
                if (s == false)
                {
                    string result;
                    result = MessageBox.Show("文件尚未保存,是否保存?", "保存文件", MessageBoxButtons.YesNoCancel).ToString();
                    switch (result)
                    {
                        case "Yes":
                            if (b == true)
                            {
                                richTextBox1.SaveFile(openFileDialog1.FileName);
                            }
                            else if (openFileDialog1.ShowDialog() == DialogResult.OK)
                            {
                                richTextBox1.SaveFile(saveFileDialog1.FileName);
                            }
                            s = true;
                            richTextBox1.Text = "";
                            break;
                        case "No":
                            richTextBox1.Text = "";
                            break;

                    }
                }
            }

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            s = false;
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (b == true || richTextBox1.Text.Trim() != "")
            {
                if (s == false)
                {
                    string result;
                    result = MessageBox.Show("文件尚未保存,是否保存?", "保存文件", MessageBoxButtons.YesNoCancel).ToString();
                    switch (result)
                    {
                        case "Yes":
                            if (b == true)
                            {
                                richTextBox1.SaveFile(saveFileDialog1.FileName);
                            }
                            else if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                            {
                                richTextBox1.SaveFile(saveFileDialog1.FileName);

                            }
                            s = true;
                            break;
                        case "No":
                            b = false;
                            richTextBox1.Text = "";
                            break;

                    }
                }
            }
              openFileDialog1.RestoreDirectory=true;
              if ((openFileDialog1.ShowDialog()==DialogResult.OK)&&openFileDialog1.FileName!="")
              {
                  richTextBox1.LoadFile(openFileDialog1.FileName);
                  b = true;
              }
                  s=true;
           /* else
            {
                openFileDialog1.InitialDirectory = @"C:\";
                openFileDialog1.AddExtension = false;
                openFileDialog1.Filter = "所有文件|*.*|文本文件(*.txt)|*.txt";
                openFileDialog1.FilterIndex = 1;
                openFileDialog1.Title = "打开文件";
                openFileDialog1.ShowHelp = true;
                openFileDialog1.ShowDialog();
                MessageBox.Show(openFileDialog1.OpenFile);
            }*/
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (b == true && richTextBox1.Modified == true)
            {
                richTextBox1.SaveFile(openFileDialog1.FileName);
                s = true;
            }
            else if (b == false && richTextBox1.Text.Trim() != "" && saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.SaveFile(saveFileDialog1.FileName);
                s = true;
                b = true;
                openFileDialog1.FileName = saveFileDialog1.FileName;
            }
        }

        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                s = true;
            }
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Undo();
        }

        private void 重复RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
        {
         
            fontDialog2.ShowColor = true;
            if (fontDialog2.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.SelectionColor = fontDialog2.Color;
                richTextBox1.SelectionFont = fontDialog2.Font;

            }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int n;
            n = toolStrip1.Items.IndexOf(e.ClickedItem);
            switch (n)
            {
                case 0:
                    新建NToolStripMenuItem_Click(sender, e); break;
                case 2:
                    保存SToolStripMenuItem_Click(sender, e); break;
                case 1:
                    打开OToolStripMenuItem_Click(sender, e); break;
                case 4:
                    剪切TToolStripMenuItem_Click(sender, e); break;
                case 5:
                    复制CToolStripMenuItem_Click(sender, e); break;
                case 6:
                    粘贴PToolStripMenuItem_Click(sender, e); break;
                case 8:
                    关于AToolStripMenuItem_Click(sender, e); break;
                
            }
        }

        private void tAbout_Click(object sender, ToolStripItemClickedEventArgs e)
        {
            throw new NotImplementedException();
        }
        private void 自动换行WToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (tAuto.Checked == false)
            {
                tAuto.Checked = true;//选中该菜单项
                richTextBox1.WordWrap = true;//设置为自动换行
            }
            else
            {
                tAuto.Checked = false;
                richTextBox1.WordWrap = false;
            }
        }

        private void 帮助HToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 新建NToolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void 打印PToolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("记事本  ");
      
        }

        private void fontDialog2_Apply(object sender, EventArgs e)
        {
            
        }

        private void 剪切UToolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void 打开OToolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void 保存SToolStripButton_Click(object sender, EventArgs e)
        {

        }

    }
}


 

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