C# C查找功能问题(无法突出显示)

C# C查找功能问题(无法突出显示),c#,winforms,find,richtextbox,C#,Winforms,Find,Richtextbox,我想问为什么我的代码不起作用 目前,我能够找到用户输入的单词,但无法在richTextBoxConversation中突出显示该单词 我该怎么做呢 以下是我的密码: private void buttonTextFilter_Click(object sender, EventArgs e) { string s1 = richTextBoxConversation.Text.ToLower(); string s2 = textBoxTextFi

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

目前,我能够找到用户输入的单词,但无法在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!");
        }
    }
您正在使用该方法-这只是告诉您单词在文本框中的位置,而不是选择它

您可以使用Find with的返回值来突出显示单词:

if (s1.Contains(s2))
{
  MessageBox.Show("Word found!");
  int wordPosition = richTextBoxConversation.Find(s2); // Get position
  richTextBoxConversation.Select(wordPosition, s2.Length);
}
int wordPosition = richTextBoxConversation.Find(s2); // Get position
if (wordPosition > -1)
{
  MessageBox.Show("Word found!");
  richTextBoxConversation.Select(wordPosition, s2.Length);
}
else
{
  MessageBox.Show("Word not found!");
}
或者,更好地避免在s1中搜索两次单词:

if (s1.Contains(s2))
{
  MessageBox.Show("Word found!");
  int wordPosition = richTextBoxConversation.Find(s2); // Get position
  richTextBoxConversation.Select(wordPosition, s2.Length);
}
int wordPosition = richTextBoxConversation.Find(s2); // Get position
if (wordPosition > -1)
{
  MessageBox.Show("Word found!");
  richTextBoxConversation.Select(wordPosition, s2.Length);
}
else
{
  MessageBox.Show("Word not found!");
}
您正在使用该方法-这只是告诉您单词在文本框中的位置,而不是选择它

您可以使用Find with的返回值来突出显示单词:

if (s1.Contains(s2))
{
  MessageBox.Show("Word found!");
  int wordPosition = richTextBoxConversation.Find(s2); // Get position
  richTextBoxConversation.Select(wordPosition, s2.Length);
}
int wordPosition = richTextBoxConversation.Find(s2); // Get position
if (wordPosition > -1)
{
  MessageBox.Show("Word found!");
  richTextBoxConversation.Select(wordPosition, s2.Length);
}
else
{
  MessageBox.Show("Word not found!");
}
或者,更好地避免在s1中搜索两次单词:

if (s1.Contains(s2))
{
  MessageBox.Show("Word found!");
  int wordPosition = richTextBoxConversation.Find(s2); // Get position
  richTextBoxConversation.Select(wordPosition, s2.Length);
}
int wordPosition = richTextBoxConversation.Find(s2); // Get position
if (wordPosition > -1)
{
  MessageBox.Show("Word found!");
  richTextBoxConversation.Select(wordPosition, s2.Length);
}
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!");
}

您可以在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!");
}

是否需要对RichTextBox.Find返回的值执行某些操作?是否需要对RichTextBox.Find返回的值执行某些操作?我不知道为什么,但它仍然没有突出显示RichTextBox中的单词。不管怎样,你能解释一下为什么是wordposition>-1吗?为什么是-1而不是任何其他数字?@athgap-如果您阅读了find的文档,我链接了它,它会告诉您如果找不到,find的返回值是-1。我不知道为什么,但它仍然没有突出显示richtextbox中的单词。不管怎样,你能解释一下为什么是wordposition>-1吗?为什么是-1而不是任何其他数字?@athgap-如果你阅读了find的文档,我链接了它,它会告诉你如果找不到,find的返回值是-1。嘿,很抱歉回复太晚了。如果我把它们放在同一个表格中,你的代码就会起作用。如果我将它们放在另一个表单中,它将无法工作。无论如何,您知道如何创建“查找下一个”函数吗?如果有超过1个相同的单词,我想有一个“查找下一个”功能,这样我就可以从richtextbox中看到该单词的其余部分被突出显示。嘿,很抱歉回复太晚了。如果我把它们放在同一个表格中,你的代码就会起作用。如果我将它们放在另一个表单中,它将无法工作。无论如何,您知道如何创建“查找下一个”函数吗?如果有超过1个相同的单词,我希望有一个查找下一个函数,这样我就可以看到该单词的其余部分在richtextbox中突出显示。