c#richtextbox查找与替换

c# 专栏收录该内容
6 篇文章 0 订阅

c#richtextbox查找与替换

接上一篇——c#窗体入门实战程序——绿色草稿纸,一键解锁打开,保存,复制,粘贴,剪切,打印,删除,全选,字体,颜色等

https://blog.csdn.net/weixin_40695088/article/details/89363270

实现时间,日期以及richtextbox的查找与替换功能  

1、或许系统日期和时间,

向Form1窗口添加一个statusStrip控件(新建在Form1的底部),在statusStrip控件的item属性添加toolStripStatusLabel1和toolStripStatusLabel2,添加Timer控件,在Tick事件写下面的代码,增加下面的代码在Form1_Load中。

        private void Form1_Load(object sender, EventArgs e)
        {
            this.timer1.Interval = 1000;
            this.timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.toolStripStatusLabel1.Text = "系统当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
        }

实现子菜单中(ToolStripMenuItem)的时间和日期功能,编写ToolStripMenuItem_Click事件

        private void 日期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("系统当日期为:" + DateTime.Now.ToString("yyyy-MM-dd "),"实时日期");
        }

        private void 时间ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("系统当时间为:" + DateTime.Now.ToString(" hh:mm:ss"), "实时时间");
        }

 

2、查找和替换功能

实现richtextbox的查找替换功能需要自己写一个窗口,再关联到主窗口中。

Form2窗口的具体代码如下,详见注释!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDI
{
    public partial class Form2 : Form
    {
        //实现对form1的关联
        Form1 mainfrom1;
        public Form2(Form1 form1)
        {
            InitializeComponent();
            mainfrom1 = form1;
        }


        private void Form2_Load(object sender, EventArgs e)
        {
            radioButton2.Checked = true;//默认开启向下查找功能
        }

        int start = 0; int sun = 0; int count = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            RichTextBox rbox = mainfrom1.RichTxtBox;
            string str = this.textBox1.Text;
            if (this.checkBox1.Checked)//是否开启区分大小写功能
            {
                this.FindDownM(rbox, str);//向下查找
            }
            else
            {
                if (this.radioButton2.Checked)
                {
                    this.FindDown(rbox, str);    
                }
                else
                {
                    this.FindUp(rbox, str);//向上查找
                }
            }
        }

        //替换textBox1中的文本为textBox2中的文本
        private void button2_Click(object sender, EventArgs e)
        {
            string str0 = this.textBox1.Text, str1 = this.textBox2.Text;
            this.replace(str0, str1);
        }

        //全部替换
        private void button3_Click(object sender, EventArgs e)
        {
            RichTextBox rbox =mainfrom1.RichTxtBox;
            string str0 = this.textBox1.Text, str1 = this.textBox2.Text;
            this.ReplaceAll(rbox, str0, str1);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //向上查找函数
        private void FindUp(RichTextBox rbox, string str)
        {
            int rbox1 = rbox.SelectionStart;
            int index = rbox.Find(str, 0, rbox1, RichTextBoxFinds.Reverse);
            if (index > -1)
            {
                rbox.SelectionStart = index;
                rbox.SelectionLength = str.Length;
                sun++;
                rbox.Focus();
            }
            else if (index < 0)
            {
                seeks(str);
                sun = 0;
               // rbox.SelectionStart = rbox.Text.Length;
            }
        }
        private void FindDown(RichTextBox rbox, string str)
        {
            int rbox1 = rbox.Text.Length;
            if (start < rbox1)
            {
                start = rbox.Find(str, start, RichTextBoxFinds.None);
                int los = rbox.SelectionStart + str.Length;
                if ((start < 0) || (start > rbox1))
                {
                    if (count == 0)
                    {
                        this.seeks(str);
                        start = los;
                        sun = 0;
                    }
                    else
                    {
                        this.seeks(str);
                        start = los;
                        sun = 0;
                    }
                }
                else if (start == rbox1 || start < 0)
                {
                    this.seeks(str);
                    start = los;
                    sun = 0;
                }
                else
                {
                    sun++;
                    start = los;
                    rbox.Focus();
                }
            }
            else if (start == rbox1 || start < 0)
            {
                int los = rbox.SelectionStart + str.Length;
                this.seeks(str);
                start = los;
                sun = 0;
            }
            else
            {
                int los = rbox.SelectionStart + str.Length;
                this.seeks(str);
                start = los;
                sun = 0;
            }
        }
        private void FindDownM(RichTextBox rbox, string str)
        {
            int rbox1 = rbox.Text.Length;
            if (start < rbox1)
            {
                start = rbox.Find(str, start, RichTextBoxFinds.MatchCase);
                int los = rbox.SelectionStart + str.Length;
                if ((start < 0) || (start > rbox1))
                {
                    if (count == 0)
                    {
                        this.seeks(str);
                        start = los;
                        sun = 0;
                    }
                    else
                    {
                        this.seeks(str);
                        start = los;
                        sun = 0;
                    }
                }
                else if (start == rbox1 || start < 0)
                {
                    this.seeks(str);
                    start = los;
                    sun = 0;
                }
                else
                {
                    sun++;
                    start = los;
                    rbox.Focus();
                }
            }
            else if (start == rbox1 || start < 0)
            {
                int los = rbox.SelectionStart + str.Length;
                this.seeks(str);
                start = los;
                sun = 0;
            }
            else
            {
                int los = rbox.SelectionStart + str.Length;
                this.seeks(str);
                start = los;
                sun = 0;
            }
        }
        //查找完毕后的弹窗
        private void seeks(string str)
        {
            if (sun != 0)
            {
                MessageBox.Show(string.Format("查找完毕,共[{0}]个\"{1}\"!", sun, str), "查找—温馨提示");
            }
            else
            {
                MessageBox.Show(String.Format("\"{0}\"!", str), "查找—温馨提示");
            }
        }
        //替换全部的函数
        private  void  ReplaceAll(RichTextBox rbox,string  str0,string str1)
        {
            rbox.Text = rbox.Text.Replace(str0, str1);
        }
        private  void replace(string  str0,string  str1)
        {
            RichTextBox rbox =mainfrom1.RichTxtBox;
            rbox.SelectionLength = str0.Length;
            rbox.SelectedText = str1;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.button1.Enabled = true;
        }
    }
}

 参考博客:https://blog.csdn.net/ou832339/article/details/23781661  

另外,需要在form1主窗口中添加

        public RichTextBox RichTxtBox
        {
            get { return this.richTextBox1; }
            set { this.richTextBox1 = value; }
        }

        //相应的查找子菜单
        private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            f2.Show();
        }
        //相应的替换子菜单
        private void 替换ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            f2.Show();
        }

 

 代码源文件下载:https://download.csdn.net/my/uploads/1/1

 

  • 3
    点赞
  • 2
    评论
  • 24
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:像素格子 设计师:CSDN官方博客 返回首页

打赏作者

朝荣

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值