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

问题描述 投票:0回答:3

我有这个代码用于在 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中查找文本。 第二个button6_Click用于查找该文本的下一个实例。 第三个按钮10_Click用于将找到的文本替换为其他文本 如果你愿意的话。我编辑了这个问题以表明我知道该怎么做。这不是微软在其有关如何使用 Richtextbox 的页面中讨论的查找方法,但它确实有效。

我将其发布在这里,供任何认为自己可以使用它的程序员使用。只需将其复制并粘贴到您的程序中即可。除了程序中已有的任何其他控件之外,您所需要的只是 3 个按钮、2 个文本框和 1 个 Richtextbox。

c# winforms replace find richtextbox
3个回答
3
投票
    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;

这就是我对此事的全部了解,希望对你有帮助:)


0
投票

通过这段代码,你可以从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;
            }
        }
    }`

0
投票

所以,我知道这是一个非常古老的问题,但我想我会添加一个现代的解决方案,可以帮助人们搜索它:

int StartIndex = 0;
/// <summary>Progressive search for text in a RichTextBox, automatically highlight and display results.</summary>
/// <param name="pSearchText">Text to Find</param>
public void FindTextInDocs(string pSearchText)
{
    try
    {
        if (!string.IsNullOrEmpty(pSearchText))
        {
            //1. Buscamos en el RTF actual:
            if (!string.IsNullOrEmpty(richTextBoxEx1.Text))
            {
                var Options = RichTextBoxFinds.None;
                if (FindMatchCase.Checked) Options |= RichTextBoxFinds.MatchCase;
                if (FindWholeWord.Checked) Options |= RichTextBoxFinds.WholeWord;

                StartIndex = richTextBoxEx1.Find(pSearchText, StartIndex, Options);
                if (StartIndex > 0)
                {
                    StartIndex += pSearchText.Length;
                }
            }
        }
    }
    catch { StartIndex = 0; } //<- Starts from begining on error
}

SearchOptions 是用户界面中某处选中的菜单(或其他内容)。

© www.soinside.com 2019 - 2024. All rights reserved.