如何在c#中的富文本框中搜索文本

时间:2016-07-19 22:40:09

标签: c# winforms replace find richtextbox

我有这个代码用于在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用于查找richtextbox中的文本。 第二个按钮6_Click用于查找该文本的下一个实例。 第三个按钮10_Click用于替换找到的文本与其他文本 如果你愿意的话。我编辑了这个问题,以表明我想出了怎么做。它不是微软在他们的页面中讨论如何使用richtextbox的查找方法,但它的工作原理。

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

快乐编码。

1 个答案:

答案 0 :(得分:1)

    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 color,删除Color.Blue,然后删除rtb.SelectionColor = color;

这就是我要解决的所有问题,希望对您有所帮助:)