• 大小: 18KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-25
  • 语言: C#
  • 标签: C#  RichTextBox  查找  替换  

资源简介

很多网友在利用RichTextBox控件作文本编辑器时,都反映不知道怎么做查找和替换,我在这里给实现一下,共享给大家吧 附件便是一个仅仅实现了查找和替换功能的文本编辑器,该功能已经完善了

资源截图

代码片段和文件信息

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

namespace MyNote
{
    public partial class FindReplaceForm : Form
    {
        private INoteTextBox m_noteTextBox = null;
        public INoteTextBox NoteTextBox
        {
            get { return this.m_noteTextBox; }
            set { this.m_noteTextBox = value; }
        }

        internal Control TextBoxControl
        {
            get { return this.m_noteTextBox as Control; }
        }

        public FindReplaceForm()
        {
            this.InitializeComponent();
        }

        private void SetButtonState()
        {
            this.btnFindNext.Enabled = false;
            this.btnReplace.Enabled = false;
            this.btnReplaceAll.Enabled = false;
            if (this.txtFindText.Text.Equals(string.Empty))
                return;
            this.btnFindNext.Enabled = true;

            if (string.Compare(this.txtFindText.Text this.txtReplaceText.Text !this.chkMatchCase.Checked) == 0)
                return;

            this.btnReplace.Enabled = true;
            this.btnReplaceAll.Enabled = true;
        }

        private void txtFindText_TextChanged(object sender EventArgs e)
        {
            this.SetButtonState();
        }

        private void txtReplaceText_TextChanged(object sender EventArgs e)
        {
            this.SetButtonState();
        }

        private void chkMatchCase_CheckedChanged(object sender EventArgs e)
        {
            this.SetButtonState();
        }

        private void btnFindNext_Click(object sender EventArgs e)
        {
            if (this.TextBoxControl == null || this.TextBoxControl.IsDisposed)
                return;
            while (true)
            {
                int result = this.m_noteTextBox.FindNext(
                    this.txtFindText.Text
                    this.chkMatchCase.Checked
                    this.chkReverseFind.Checked
                );
                if (result >= 0)
                    break;

                string szMsgInfo = null;
                if (this.chkReverseFind.Checked)
                    szMsgInfo = “已经查找到文档开始!是否重新从文档末尾处查找?“;
                else
                    szMsgInfo = “已经查找到文档结尾!是否重新从文档开始处查找?“;
                DialogResult dlgResult = MessageBox.Show(szMsgInfo “查找和替换“ MessageBoxButtons.YesNo MessageBoxIcon.Question);
                if (dlgResult != DialogResult.Yes)
                    break;
                if (this.chkReverseFind.Checked)
                    this.m_noteTextBox.Selectionstart = this.m_noteTextBox.TextLength;
                else
                    this.m_noteTextBox.Selectionstart = 0;
            }
        }

        private void btnReplace_Click(object sender EventArgs e)
        {
            if (this.TextBoxControl == null || this

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       4861  2008-12-21 23:37  MyNote\FindReplaceForm.cs

     文件       7589  2008-12-21 23:37  MyNote\FindReplaceForm.Designer.cs

     文件       5814  2008-12-21 23:37  MyNote\FindReplaceForm.resx

     文件        723  2008-12-21 23:04  MyNote\MainForm.cs

     文件       4259  2008-12-21 23:04  MyNote\MainForm.Designer.cs

     文件       6012  2008-12-21 23:04  MyNote\MainForm.resx

     文件       3715  2008-12-21 21:40  MyNote\MyNote.csproj

     文件       2872  2008-12-21 23:34  MyNote\NoteTextBox.cs

     文件        476  2008-12-21 15:20  MyNote\Program.cs

     文件       1266  2008-12-21 15:17  MyNote\Properties\AssemblyInfo.cs

     文件       2839  2008-12-21 15:17  MyNote\Properties\Resources.Designer.cs

     文件       5612  2008-12-21 15:17  MyNote\Properties\Resources.resx

     文件       1089  2008-12-21 15:17  MyNote\Properties\Settings.Designer.cs

     文件        249  2008-12-21 15:17  MyNote\Properties\Settings.settings

     文件        907  2008-12-21 15:17  MyNote.sln

    ..A..H.     26624  2008-12-21 23:39  MyNote.suo

     目录          0  2008-12-21 15:17  MyNote\Properties

     目录          0  2008-12-21 23:39  MyNote

----------- ---------  ---------- -----  ----

                74907                    18


评论

共有 条评论