【问题标题】:Searching string in richtextbox在富文本框中搜索字符串
【发布时间】:2012-08-23 13:06:28
【问题描述】:

这是我在我的 Richtextbox 中查找文本并突出显示它的代码:

        int start = 0;
        int indexOfSearchText = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            int startindex = 0;

            if (textBox1.Text.Length > 0)
                startindex = FindMyText(textBox1.Text.Trim(), start, richTextBox1.Text.Length);

            // If string was found in the RichTextBox, highlight it
            if (startindex >= 0)
            {
                // Set the highlight color as red
                richTextBox1.SelectionBackColor = Color.Red;
                // Find the end index. End Index = number of characters in textbox
                int endindex = textBox1.Text.Length;
                // Highlight the search string
                richTextBox1.Select(startindex, endindex);
                richTextBox1.ScrollToCaret();
                // mark the start position after the position of
                // last search string
                start = startindex + endindex;
            }

            else
            {
                MessageBox.Show("0 occurence in richtextbox");
            }
        }

        public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
        {
            // Unselect the previously searched string
            if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
            {
                richTextBox1.Undo();
            }

            // Set the return value to -1 by default.
            int retVal = -1;

            // A valid starting index should be specified.
            // if indexOfSearchText = -1, the end of search
            if (searchStart >= 0 && indexOfSearchText >= 0)
            {
                // A valid ending index
                if (searchEnd > searchStart || searchEnd == -1)
                {
                    // Find the position of search string in RichTextBox
                    indexOfSearchText = richTextBox1.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
                    // Determine whether the text was found in richTextBox1.
                    if (indexOfSearchText != -1)
                    {
                        // Return the index to the specified search text.
                        retVal = indexOfSearchText;
                    }
                }
            }
            return retVal;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            start = 0;
            indexOfSearchText = 0;
        }
    }
}

问题是,当它到达richtextBox1的末尾时,它并没有从头开始重新启动!我想在到达richtextBox1.text 的末尾后再次循环到开始。

【问题讨论】:

    标签: c# .net find richtextbox


    【解决方案1】:

    这个怎么样?

    if (textBox1.Text.Length > 0)
    {
        startindex = FindMyText(textBox1.Text.Trim(), start, richTextBox1.Text.Length);
        if(startindex == -1 && start > 0) // Not found string and not searching from beginning
        {
            // Wrap search
            int oldStart = start;
            start = 0;
            startindex = FindMyText(textBox1.Text.Trim(), start, oldStart);
        }
    }
    

    【讨论】:

      【解决方案2】:

      下面的代码对于在richtextbox中搜索文本并以圆形方向突出显示文本很有用。我在richtextbox.i中使用文本框,我在文本框中输入搜索文本并按回车按钮查找下一个。最初我,m将 start 和 indexOfSearchText 的两个值声明为 0。

      int 开始 = 0; int indexOfSearchText = 0;

      private void textBox1_KeyDown(object sender, KeyEventArgs e)

           private void textBox1_KeyDown(object sender, KeyEventArgs e)   
              {
                  if (e.KeyCode == Keys.Enter)
      
      
                  {                   
                       int startindex = 0;
      
                      if (textBox1.Text.Length > 0)
                      {
      
      
                          startindex = FindMyText(textBox1.Text.Trim(), start, richTextBox1.Text.Length);
      
                          if (startindex == -1 && start >= 0) // Not found string and not searching from beginning
                          {
                              // Wrap search
                             // int oldStart = start;
                              start = 0;
                              startindex = FindMyText(textBox1.Text.Trim(), start, richTextBox1.Text.Length);
                          }
      
      
                      }
      
      
                      // If string was found in the RichTextBox, highlight it
                      if (startindex >= 0)
                      {
                          // Set the highlight color as red
                          richTextBox1.Select(startindex, textBox1.TextLength);
                          richTextBox1.SelectionBackColor = Color.Yellow;
                         // richTextBox1.SelectionColor = Color.Red;
                          // Find the end index. End Index = number of characters in textbox
                          int endindex = textBox1.Text.Length;
                          // Highlight the search string
                          richTextBox1.Select(startindex, endindex);
                          // mark the start position after the position of
                          // last search string
                          start = startindex + endindex;
                      }
      
                  }
              }
      
      
      
              public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
              {
                  // Unselect the previously searched string
                  if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
                  {
                      richTextBox1.Undo();
                  }
      
                  // Set the return value to -1 by default.
                  int retVal = -1;
      
                  // A valid starting index should be specified.
                  // if indexOfSearchText = -1, the end of search
                  if (searchStart >= 0 && indexOfSearchText >= 0)
                  {
                      // A valid ending index
                      if (searchEnd > searchStart || searchEnd == -1)
                      {
                          // Find the position of search string in RichTextBox
                          indexOfSearchText = richTextBox1.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
                          // Determine whether the text was found in richTextBox1.
                          if (indexOfSearchText != -1)
                          {
                              // Return the index to the specified search text.
                              retVal = indexOfSearchText;
                          }
                          else
                          {
                              start = 0;
                              indexOfSearchText = 0;
                              //return indexOfSearchText;
                          }
                      }
                  }
                  return retVal;
      
              }
      
       private void textBox1_TextChanged(object sender, EventArgs e)
              {
      
      
                  start = 0;
                  indexOfSearchText = 0;
                  string temp = richTextBox1.Text;
                  richTextBox1.Text = string.Empty;
                  richTextBox1.Text = temp;
                  richTextBox1.SelectionBackColor = Color.White;
              }
      

      【讨论】:

      • 请花时间正确格式化您的答案并添加解释。
      猜你喜欢
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 1970-01-01
      相关资源
      最近更新 更多