richTextBoxConversation

richTextBoxConversation

我想问为什么我的代码不起作用?

目前,我能够找到用户输入的单词,但无法在richTextBoxConversation中突出显示该单词。

我应该怎么做呢?

以下是我的代码:

    private void buttonTextFilter_Click(object sender, EventArgs e)
    {
        string s1 = richTextBoxConversation.Text.ToLower();
        string s2 = textBoxTextFilter.Text.ToLower();

        if (s1.Contains(s2))
        {
            MessageBox.Show("Word found!");
            richTextBoxConversation.Find(s2);
        }
        else
        {
            MessageBox.Show("Word not found!");
        }
    }

最佳答案

您可以在RichTextBox中选择文本,但是如果该Richtextbox具有焦点,则需要始终记住该文本将处于选定模式,因此您的代码必须为

// RichTextBox.Select(startPos,length)

int startPos = richTextBoxConversation.Find(s2);

int length = s2.Length;

if (startPos > -1)
{
    MessageBox.Show("Word found!");
    // Now set focus on richTextBox
    richTextBoxConversation.Focus();
    richTextBoxConversation.Select(startPos , length );
}
else
{
    MessageBox.Show("Word not found!");
}

关于c# - C#查找功能问题(无法突出显示),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4264471/

10-10 18:18