Searching for Text

How can I search for Text in TextBox or in Rich TextBox

like that Find button in notepad and Wordpad

Please help in this and tell me if it just code or if there is a tool to do that.

Thanks.




Answer this question

Searching for Text

  • Kumar80

    hi,

    thats very good that you knew the answer,

    should i write all that i just gave you a reference for the solution , don't forget i said "you can polish it but this works", i'm not trying to write the project for you , i just trying to help

    ok regarding to your other question this is another solution you can change  this line

    int tmp = textBox1.Text.ToLower().IndexOf(textBox2.Text.ToLower(), startindex + 1);

    i changed the both of textboxs.text tolower so if the user enter a capital letter or the text contain a capital letter both of them will be ignored during searching

    best regards

    Shakalama



  • Aaron Humphrey

    ok Marked

    Thank you

    have a nice time

    good luck :)



  • vpdsouza

    Try this:

    int tmp = textBox1.Text.IndexOf(textBox2.Text, startindex + 1, StringComparison.CurrentCultureIgnoreCase);

     

    The default is :

    StringComparison.CurrentCulture



  • Boris B

    hi,

    Thanks shakalama for your help,

    this code is good but there is somethings I think we have to do...
    fristly this code will search for just 1 Time then you cannot change the string to another and search for it so..
    we have to put ' startindex = 0;' under textBox2_TextChanged
    this will reset the value of startindex then we can search again for another.

    you know Windows`s users dont care of the statue of letters(Capital or Small) but with this code you must enter the string exactly like that in the textBox1.. so if you have any idea to solve this problem and control it like notepad plz tell me.

    Best Regards.



  • wjousts

    hi,

    you can do something like that


    namespace WindowsApplication1
    {
    //this form contain

    // multilines textbox to display the text

    //textbox for user to write the search critarea

    //button to find the next match by every click

    public partial class Form1 : Form

    {
    public Form1()
    {
    InitializeComponent();
    this.textBox1.AcceptsReturn = true;
    this.textBox1.HideSelection = false;
    this.textBox1.Multiline = true;
    textBox1.Text =
    " Hello, Thanks for your reply now I understood what you had say and I did it with 'Contains'but I cannot apply that with 'IndexOf'so plz explain it form me-------------another somthing,when I get the string that I`m looking for, How can I make a selection on it and how can I start searching starting from the string that I found before (Long questions I think )Thanks.";
    }
    int startindex = 0;
    private void button1_Click(object sender, EventArgs e)
    {
    int tmp = textBox1.Text.IndexOf(textBox2.Text, startindex + 1);
    if (tmp > -1 && textBox2.Text.Length > 0)
    {
    startindex = tmp;
    textBox1.Select(startindex, textBox2.Text.Length);
    }
    }
    }
    }


    you can polish it more than that but this works

    best regards



  • Herr Vorragend

    Hello,

    Thanks for your reply

    now I understood what you had say and I did it with 'Contains'

    but I cannot apply that with 'IndexOf'

    so plz explain it form me

    -------------

    another somthing,

    when I get the string that I`m looking for, How can I make a selection on it and how can I start searching starting from the string that I found before

    (Long questions I think )

    Thanks.



  • kbarlar

    hi,

    notanotherusername,

    may be converting to Lower will take longer time specially if the file was too long, so that I used

    CurrentCultureIgnoreCase

    as shakalama wrote and its working good now

    and I have a problems with reading some files, I will write it in other topic shortly

    Best Regards



  • Shifarad

    hi,

    it`s really good

    you solved this problem

    about this line I made the same as this line

    int tmp = textBox1.Text.ToLower().IndexOf(textBox2.Text.ToLower(), startindex + 1);

    but I wrote it like this :

    int tmp = textBox1.Text.IndexOf(textBox2.Text.ToLower(), startindex + 1);

    so that cased a problem with me but now it works excellent

    There is another problem I will try to fix it and if I failed I will write it here

    Thank you



  • Tinu

    Thankyou all:) This works perfect for me.! Thank you!!

    Best Regards!!

    Rickard

  • VisualDBA

    Hehe hi, I have searched for a sulotion for sooo long time something simpel like this. And this acctuly works but there is one litte tiny problem.. When i search for my word it only highlights in the textbox but dosent scroll down to the word so you have to scroll down by hand to the highlighted word. I have probably missed something simpel, anyone knows what

    Best Regards

    //Rickard [swe]

  • JohnCronin

    hi

    AeQuitaZ

    I found the same problem but I solved it

    simply put this code at the end of your search code

    textBox1.ScrollToCaret();

    this will scroll down to your highlighted text

    Best Regards



  • 勇敢的心

    hi,

    because your problem was solved and you decided to start a new thread , would you mark the answer for your question

    thx



  • denis kahl

    I'm not sure using ToLower() is a good idea, surely that will cause the entire contents of the textbox to be copied in memory to a lowcase version.

    Testing it out on my box it is definately slower, twice to three times if you don't scroll as well. Not a noticable difference, but would scale worse and eat RAM on large files.



  • Brandon Hawkins

    Use the 'Contains' or the 'IndexOf' methods of the Text property of the textbox or rich textbox to search for strings.

    'Contains' will return a boolean value indicating whether a string contains a particular substring.

    'IndexOf' is more versatile & returns the index of the substring.


  • Searching for Text