Ask a questionAsk a question
 

AnswerFind Functionality

  • Wednesday, July 01, 2009 8:46 PMJay Mazz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How can I add a control / menu / etc that enables find functionality?  I have a text box populated with the contents of a text file.  I'd like to give the user the ability to search that text box.
    • Moved byOmegaManMVPWednesday, July 01, 2009 9:01 PM (From:Visual C# General)
    •  

Answers

  • Wednesday, July 01, 2009 8:55 PMDeborahKMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    This worked for me:

                string textToFind = "is";
                int findPosition = textBox2.Text.IndexOf(textToFind);
                textBox2.Focus();
                textBox2.SelectionStart = findPosition;
                textBox2.SelectionLength = textToFind.Length;
    

    You would just need to set textToFind to whatever the user typed in.

    Hope this helps.


    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked As Answer byJay Mazz Wednesday, July 01, 2009 9:03 PM
    •  

All Replies

  • Wednesday, July 01, 2009 8:48 PMDeborahKMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How did you want it to work? Just move the focus to the found text?


    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
  • Wednesday, July 01, 2009 8:49 PMJay Mazz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes, highlighting would be great but isn't necessary.
  • Wednesday, July 01, 2009 8:55 PMDeborahKMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    This worked for me:

                string textToFind = "is";
                int findPosition = textBox2.Text.IndexOf(textToFind);
                textBox2.Focus();
                textBox2.SelectionStart = findPosition;
                textBox2.SelectionLength = textToFind.Length;
    

    You would just need to set textToFind to whatever the user typed in.

    Hope this helps.


    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked As Answer byJay Mazz Wednesday, July 01, 2009 9:03 PM
    •  
  • Wednesday, July 01, 2009 9:00 PMJay Mazz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a menu strip to which I added 'Find'.  When I click 'find', I'd like to pop open an input box (which I'll use to set textToFind).  How can I pull that off?
  • Wednesday, July 01, 2009 9:02 PMFernando Nicolet Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You can make your own Search Form for that.

    Anyways, you may want to add this de Deborah's answer:

    textBox2.ScrollToCaret();

    I always try to Keep it Sharp & simple.
  • Wednesday, July 01, 2009 9:08 PMDeborahKMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Here is an example of an input box:

    http://www.csharp-examples.net/inputbox/

    Otherwise, you could just add a second TextBox to your form that displays the Text and have a Search button. Then it would look more like IE's search.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!