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

t9eec4r0  于 2023-10-23  发布在  其他
关注(0)|答案(3)|浏览(123)

我有这样的代码在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();
    }

第一个button5_Click用于在richtextbox中查找文本。第二个button6_Click用于查找该文本的下一个示例。第三个button10_Click用于替换找到的文本,如果你愿意的话。我编辑了这个问题,以表明我知道如何做到这一点。这不是微软在他们关于如何使用文本框的页面中谈论的查找方法,但它确实有效。
我把这个贴在这里给任何觉得他们可以使用它的程序员。只需将其复制并粘贴到您的程序中。所有你需要的是3个按钮和两个文本框和一个richtextbox除了任何其他控制你已经在你的程序。

0ejtzxu1

0ejtzxu11#

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;
这就是我对这个问题的看法,希望对你有所帮助:)

yi0zb3m4

yi0zb3m42#

使用此代码,您可以从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;
            }
        }
    }`
bkhjykvo

bkhjykvo3#

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

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是你UI中的一个菜单(或者其他什么)。

相关问题