如何在富文本框中搜索文本

本文关键字:文本 搜索 | 更新日期: 2023-09-27 17:57:40

我有这个代码用于在Rtf框中查找文本并突出显示文本。

public Form1()
{
InitializeComponent();
}
public int currentPos = 1; // this is so currentPos does not loose its value

然后我有三个事件与类似的按钮相关

private void button5_Click(object sender, EventArgs e)
    {
       
        currentPos = 1;
        if (!string.IsNullOrEmpty(this.textBox1.Text))
        {
            if (this.richTextBox1.Text.Contains(this.textBox1.Text) && currentPos < this.richTextBox1.Text.Length)
            {
                if (this.richTextBox1.Text.Substring(currentPos).Contains(this.textBox1.Text))
                {
                    int start = this.richTextBox1.Text.Substring(currentPos).IndexOf(this.textBox1.Text);
                    this.richTextBox1.Select(start + currentPos, this.textBox1.Text.Length);
                    currentPos = start + currentPos + this.textBox1.Text.Length;
                    this.richTextBox1.Focus();
                }
                else
                {
                    //restart the method after resetting the indicator
                    button5.Text = "Find Next";
                    button5_Click(button5, new EventArgs());
                }
            }
            else
            {
                //restart the method after resetting the indicator
                currentPos = currentPos + 1;
                if (this.richTextBox1.Text.Contains(this.textBox1.Text))
                {
                 
                    button5_Click(button5, new EventArgs());
                }
                else
               
                    MessageBox.Show("Text not found");
            }
        }
    
     }
    // same as the previous code for button5_Click except currentPos
    // does not start over, it keeps searching from where it found the
    // last text
    private void button6_Click(object sender, EventArgs e)
    {
             if (!string.IsNullOrEmpty(this.textBox1.Text))
        {
            if (this.richTextBox1.Text.Contains(this.textBox1.Text) && currentPos < this.richTextBox1.Text.Length)
            {
                if (this.richTextBox1.Text.Substring(currentPos).Contains(this.textBox1.Text))
                {
                    int start = this.richTextBox1.Text.Substring(currentPos).IndexOf(this.textBox1.Text);
                    this.richTextBox1.Select(start + currentPos, this.textBox1.Text.Length);
                    currentPos = start + currentPos + this.textBox1.Text.Length;
                    this.richTextBox1.Focus();
                }
                else
                {
                
                    DialogResult dialogResult = MessageBox.Show( "Larry's Journal has finished searching through the document. Do you want to continue the search from the top of the document?", "Message", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        currentPos = 1;
                        button5_Click(button5, new EventArgs());
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        //do something else
                    }
                }
            }
           else
            {
                //restart the method after resetting the indicator
                currentPos = currentPos + 1;
                if (this.richTextBox1.Text.Contains(this.textBox1.Text))
                    button1_Click(button5, new EventArgs());
                else
                    MessageBox.Show("Text not found");
            }
        }
    }
 // This replaces the selected text in the richtextbox with the contents
 // of texxtBox2 which is the replacement text.
 private void button10_Click(object sender, EventArgs e)
    {
       textBox2.SelectAll();
        textBox2.Copy();
        richTextBox1.Paste();
    }

第一个按钮5_Click用于在richtext框中查找文本。第二个按钮6_Click用于查找该文本的下一个实例。第三个按钮10_Click用于将找到的文本替换为其他文本如果你愿意的话。我编辑了这个问题,以表明我已经找到了如何做到这一点。这不是微软在其页面中谈论的如何使用richtextboxes的查找方法,但它是有效的。

我把这个贴在这里给任何觉得可以使用的程序员。只需复制并粘贴到你的程序中。除了程序中已有的任何其他控件外,您只需要3个按钮、两个文本框和一个richtextbox。

如何在富文本框中搜索文本

    public static void Find(RichTextBox rtb, String word, Color color)
    {
        if (word == "")
        {
            return;
        }
        int s_start = rtb.SelectionStart, startIndex = 0, index;
        while ((index = rtb.Text.IndexOf(word, startIndex)) != -1)
        {
            rtb.Select(index, word.Length);
            rtb.SelectionColor = color;
            startIndex = index + word.Length;
        }
        rtb.SelectionStart = 0;
        rtb.SelectionLength = rtb.TextLength;
        rtb.SelectionColor = Color.Black;
    }

所以,基本上,这就是"查找"或"搜索"方法。但是,我还没有向你展示如何使用它:)这里:

    private void button1_Click(object sender, EventArgs e)
    {
        Find(richtext, textBox1.Text, Color.Blue);
    }

RichTextBox替换为richtext,并将Color.Blue替换为您选择的Color。如果搜索到的文本不是来自TextBox,请将textBox1.Text替换为其他Control

如果您最终不想要Color,则移除Color colorColor.Bluertb.SelectionColor = color;

这就是我对这件事的全部看法,我希望它能帮助你:)

使用此代码,您可以从RichTextBox设置的焦点开始位置选择要搜索的文本,此外还可以提供一些搜索条件,例如在NotePad for Windows 中找到的条件

private void button1_Click(object Sender, EventArgs e)
    {
        string s1 = textBox1.Text;
        `int` a = richTextBox1.SelectionStart + 1;
        if (radioButton1.Checked == true &&
            (checkBox1.Checked == false && checkBox2.Checked == false))
        {
            `int` `startpos` = richTextBox1.Find(s1.ToCharArray(), a);
            `int` `length` = s1.Length; a = `startpos`;
            richTextBox1.Focus();
            if (`startpos` > -1)
            {
                richTextBox1.Select(`startpos`, textBox1.Text.Length);
            }
            else
            {
                richTextBox1.SelectionStart = 0;
                `MessageBox.Show`("Finish Find" + textBox1.Text, "Info", `MessageBoxButtons.OK`, `MessageBoxIcon.Information`);
            }
        }

        else if (radioButton1.Checked == true && (checkBox1.Checked == true && checkBox2.Checked == true))
        {
            `int` position = richTextBox1.SelectionStart;
            `int` b = richTextBox1.Text.LastIndexOf(s1, position - 1, `StringComparison.CurrentCulture`);
            if (b > -1)
            {
                richTextBox1.Focus();
                richTextBox1.Select(b, s1.Length);
            }
            else
            {
                b = richTextBox1.Text.Length;
                richTextBox1.SelectionStart = richTextBox1.Text.Length - 1;
            }
        }

        else if (radioButton1.Checked == true && (checkBox1.Checked == true && checkBox2.Checked == false))
        {
            `int` position = richTextBox1.SelectionStart;
            `int` b = richTextBox1.Text.LastIndexOf(s1, position - 1, `StringComparison.CurrentCulture`);
            if (b > -1)
            {
                richTextBox1.Focus();
                richTextBox1.Select(b, s1.Length);
            }
            else
            {
                b = richTextBox1.Text.Length;
                `MessageBox.Show`("Finish Find" + textBox1.Text, "Info", `MessageBoxButtons.OK`, `MessageBoxIcon.Information`);
            }
        }

        else if (radioButton1.Checked == true && (checkBox1.Checked == false && checkBox2.Checked == true))
        {
            `int` position = richTextBox1.SelectionStart;
            `int` b = richTextBox1.Text.LastIndexOf(s1, position - 1);
            if (b > -1)
            {
                richTextBox1.Focus();
                richTextBox1.Select(b, s1.Length);
            }
            else
            {
                b = richTextBox1.Text.Length;
                richTextBox1.SelectionStart = richTextBox1.Text.Length - 1;
            }
          `  //int startpos = richTextBox1.Find(textBox1.Text, a + 1, RichTextBoxFinds.WholeWord);
            //int leanth = s1.Length; a = startpos;
            //richTextBox1.Focus();
            //richTextBox1.Select(startpos, textBox1.Text.Length);`
        }
        else if (radioButton2.Checked == true && (checkBox1.Checked == true && checkBox2.Checked == true))
        {
          `  int position = richTextBox1.SelectionStart;
            int b = richTextBox1.Find(s1, position + 1, RichTextBoxFinds.MatchCase);`
            if (b > -1)
            {
                richTextBox1.Focus();
                richTextBox1.Select(b, s1.Length);
            }
            else
            {
                b = richTextBox1.Text.Length;
                richTextBox1.SelectionStart = richTextBox1.Text.Length - 1;
            }
        }
        else if (radioButton2.Checked == true && (checkBox1.Checked == true && checkBox2.Checked == false))
        {
          `  int position = richTextBox1.SelectionStart;
            int b = richTextBox1.Find(s1, position + 1, RichTextBoxFinds.MatchCase);`
            if (b > -1)
            {
                richTextBox1.Focus();
                richTextBox1.Select(b, s1.Length);
            }
            else
            {
                b = richTextBox1.Text.Length;
                Message Box .Show("Finish Find" + textBox1.Text, "Info", Message Box Buttons .OK, Message Box Icon .Information);
            }
        }
        else if (radioButton2.Checked == true && (checkBox1.Checked == false && checkBox2.Checked == true))
        {
            `int` position = richTextBox1.SelectionStart;
            `int` b = richTextBox1.Find(s1, position + 1, Rich Text Box Finds . None);
            if (b > -1)
            {
                richTextBox1.Focus();
                richTextBox1.Select(b, s1.Length);
            }
            else
            {
                b = richTextBox1.Text.Length;
                richTextBox1.SelectionStart = richTextBox1.Text.Length - 1;
            }
        }
    }`