记事本 C#实现 以及一些细节

项目地址:https://github.com/lianghowe/My_editor
项目效果:
在这里插入图片描述

  • 文件
    新建
    保存
    打开
    另存为
    页面设置
    打印
    退出
  • 编辑功能
    撤销
    剪切
    复制
    粘帖
    删除
    查找
    查找下一个
    替换
    转到
    全选
    时间/日期
  • 格式
    自动换行
    字体
    颜色
  • 查看
    状态栏
  • 帮助
    关于
    帮助
  1. 打开文件,保存文件
        //打开
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            //过滤器
            openFileDialog1.Filter = "文本文件|*.txt|所有文件|*.*";
            if(openFileDialog1.ShowDialog()== DialogResult.OK)
            {
                filename = openFileDialog1.FileName;

                //加载文件
                richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);
                // 路径只剩文件名
                this.Text = filename.Substring(filename.LastIndexOf("\\") + 1) + " - My_editor";
            }
        }

        //保存
        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            if (filename.Length > 0)
                richTextBox1.SaveFile(filename, RichTextBoxStreamType.PlainText);

            //新文件另存 
            else
                toolStripMenuItem5_Click(sender, e);
        }

        //另存为
        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "文本文件|*.txt|所有文件|*.*";
            if(saveFileDialog1.ShowDialog()==DialogResult.OK)
            {

                filename = saveFileDialog1.FileName;
                // 路径只剩文件名
                filename = filename.Substring(filename.LastIndexOf("\\") + 1);
                richTextBox1.SaveFile(filename, RichTextBoxStreamType.PlainText);

                this.Text = filename + " - My_editor";
            }
        }
  1. 查找
        //查找_click
        private void toolStripMenuItem16_Click(object sender, EventArgs e)
        {
            new find(this).Show();
        }

        //查找功能
        public void find(string find_string, bool is_up, bool is_upper)
        {
            //已经查找到文本底部,弹出用户提示
            if (position>=richTextBox1.Text.Length)
            {
                MessageBox.Show("已到文本底部,再次查找将回到顶部", "提示");
                position = 0;
                return;
            }

            //向上 
            if(is_up)
            {   
                // 此处反转一下 0 为终点,position 为起点
                position = richTextBox1.Find(find_string, 0, position, RichTextBoxFinds.Reverse);

                //找不到
                if (position == -1)
                {
                    MessageBox.Show("找不到 " + find_string);
                    position = richTextBox1.TextLength;
                }

                else
                {
                    richTextBox1.Focus();
                }
            }

            //向下 
            else
            {
                //区分大小写
                if(is_upper)
                {
                    position = richTextBox1.Find(find_string, position, RichTextBoxFinds.MatchCase);
                }

                // 不分大小写
                else
                {
                    position = richTextBox1.Find(find_string, position, RichTextBoxFinds.None);
                }

                //找不到
                if (position == -1)
                {
                    MessageBox.Show("找不到 " + find_string);
                    position = 0;
                }

                else
                {
                    richTextBox1.Focus();
                    position += find_string.Length;
                }
            }
        }
  1. 替换
        //替换-click
        private void toolStripMenuItem17_Click(object sender, EventArgs e)
        {
            new replace(this).Show();
        }

        //替换功能
        public void replace(string replace_string)
        {
            if (richTextBox1.SelectedText.Length != 0)
            {
                richTextBox1.SelectedText = replace_string;
            }
        }

        //全部替换 
        public void replace_all(string find_string,string replace_string,bool is_upper_lower)
        {
            // 此处用 Regex 进行替换,RegexOptions.None 为不忽略大小写,RegexOptions.IgnoreCase 为忽略大小写
            if(is_upper_lower)
                richTextBox1.Text=Regex.Replace(richTextBox1.Text, find_string, replace_string, RegexOptions.None);
            else
                richTextBox1.Text=Regex.Replace(richTextBox1.Text, find_string, replace_string, RegexOptions.IgnoreCase);
        }
  1. 插入时间日期
        //时间/日期
        private void 时间日期DToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText = DateTime.Now.ToString();
        }
  1. 获得行列
        //获得行列
        private void row_line(object sender, EventArgs e)
        {
            // richTextBox 对所有内容建立了索引。
            int index = richTextBox1.GetFirstCharIndexOfCurrentLine();
            
            int line = richTextBox1.GetLineFromCharIndex(index) + 1;
           
            int column = richTextBox1.SelectionStart - index + 1;
            this.label2.Text = string.Format("第 {0}行, {1}列", line.ToString(), column.ToString());
        }
  1. 帮助
        //帮助
        private void toolStripMenuItem24_Click(object sender, EventArgs e)
        {
            // 浏览器打开链接
            System.Diagnostics.Process.Start("https://blog.csdn.net/welcom_/article/details/84898056");
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值