none
System.Windows.Forms.Textbox - Need Help With Highlighting Characters RRS feed

  • Question

  • I am using a System.Windows.Forms.Textbox to display a flatwire message.  By using seperators "~" inside the flatwire message, the code is dividing the flatwire message into several different lines and displaying it in the textbox. 

    I am now adding Search functionality for the textbox.  For example, If a user enters the characters "BHT", I want to highlight the instance of "BHT" in the textbox.  How can I make my code highlight certain characters inside a System.Windows.Forms.Textbox?

     

    Thursday, March 10, 2011 9:44 PM

Answers

  • If you want to use normal textbox, I don't think you can highlight multiple occurances of searched text. Below is the code where very first matched word is highlighted.

    private void btnSearchWord_Click(object sender, EventArgs e)
    {
     if (txtSearch.Text.Length > 0)
     {
      int index = txtMultiLine.Text.IndexOf(textBox1.Text);
      if (index > -1)
      {
       txtMultiLine.HideSelection = false;
       txtMultiLine.SelectionStart = index;
       txtMultiLine.SelectionLength = txtSearch.TextLength;
      }
     }
    }
    

    But I suggest you use RichTextBox for such purposes. If you use RichTextBox, you can highlight multiple occurences of the searched text and you do the things as below.

    private void btnSearchWords_Click(object sender, EventArgs e)
    {   
     if (txtSearch.Text.Length > 0)
     {
      int searchedIndex = rtf.Text.IndexOf(txtSearch.Text, StringComparison.InvariantCultureIgnoreCase);
      while (searchedIndex > -1)
      {
       rtf.HideSelection = false;
       rtf.SelectionStart = searchedIndex;
       rtf.SelectionLength = textBox1.TextLength;
       rtf.SelectionBackColor = Color.Aqua;
       rtf.SelectionProtected = true;
       searchedIndex = rtf.Text.IndexOf(txtSearch.Text, searchedIndex + txtSearch.TextLength + 1, StringComparison.InvariantCultureIgnoreCase);
      }
      rtf.SelectionStart = 0;
      rtf.SelectionLength = 0;
     }
    }
    

    Hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming !!!

    • Proposed as answer by Kris Anderson Tuesday, March 15, 2011 12:05 PM
    • Marked as answer by Paul Zhou Friday, March 18, 2011 1:53 AM
    Thursday, March 10, 2011 11:39 PM

All replies

  • Try http://social.msdn.microsoft.com/Forums/en-US/category/windowsforms

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Thursday, March 10, 2011 11:36 PM
  • If you want to use normal textbox, I don't think you can highlight multiple occurances of searched text. Below is the code where very first matched word is highlighted.

    private void btnSearchWord_Click(object sender, EventArgs e)
    {
     if (txtSearch.Text.Length > 0)
     {
      int index = txtMultiLine.Text.IndexOf(textBox1.Text);
      if (index > -1)
      {
       txtMultiLine.HideSelection = false;
       txtMultiLine.SelectionStart = index;
       txtMultiLine.SelectionLength = txtSearch.TextLength;
      }
     }
    }
    

    But I suggest you use RichTextBox for such purposes. If you use RichTextBox, you can highlight multiple occurences of the searched text and you do the things as below.

    private void btnSearchWords_Click(object sender, EventArgs e)
    {   
     if (txtSearch.Text.Length > 0)
     {
      int searchedIndex = rtf.Text.IndexOf(txtSearch.Text, StringComparison.InvariantCultureIgnoreCase);
      while (searchedIndex > -1)
      {
       rtf.HideSelection = false;
       rtf.SelectionStart = searchedIndex;
       rtf.SelectionLength = textBox1.TextLength;
       rtf.SelectionBackColor = Color.Aqua;
       rtf.SelectionProtected = true;
       searchedIndex = rtf.Text.IndexOf(txtSearch.Text, searchedIndex + txtSearch.TextLength + 1, StringComparison.InvariantCultureIgnoreCase);
      }
      rtf.SelectionStart = 0;
      rtf.SelectionLength = 0;
     }
    }
    

    Hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming !!!

    • Proposed as answer by Kris Anderson Tuesday, March 15, 2011 12:05 PM
    • Marked as answer by Paul Zhou Friday, March 18, 2011 1:53 AM
    Thursday, March 10, 2011 11:39 PM