locked
Vb.net search for string/word in strings

    Question

  • Hello!

    This i what i have so far:

    Dim Search As String
        Dim Where As String
        ' Get search string from user.
        Search = searchtextbox.text
        ' Find string in text.
        Where = InStr(RichTextBox1.Text, Search)
    
        If Where Then
          RichTextBox1.Focus()
          RichTextBox1.SelectionStart = Where - 1
          RichTextBox1.SelectionLength = Len(Search)
          highlighted!", MsgBoxStyle.Information)
        Else
          'MsgBox("No strring found", MsgBoxStyle.Critical)
    
        End If
    

    this search works and search up the input search word however it will only find the first! so if i got text with lot of lines and some words repeat the first one will only be highlighted.

     

    So is there a way to have a search method like internet explorer? every time i click next the program will find the NEXT term or word.

     

    Thank you!


    If you don´t Believe in it, Then it Doesn´t Exist!
    Monday, December 06, 2010 8:32 PM

Answers

  • well, im going to post the code to highlighting all the text you search for at the same time but if that is not what you want, you have to solve it on your own.

    Sub findTextAndHighlight(ByVal searchtext As String, ByVal rtb As RichTextBox)
        Dim textEnd As Integer = rtb.TextLength
        Dim index As Integer = 0
        Dim fnt As Font = New Font(rtb.Font, FontStyle.Bold)
        Dim lastIndex As Integer = rtb.Text.LastIndexOf(searchtext)
        While (index < lastIndex)
          rtb.Find(searchtext, index, textEnd, RichTextBoxFinds.WholeWord)
          rtb.SelectionFont = fnt
          rtb.SelectionLength = searchtext.Length
          rtb.SelectionColor = Color.Red
          rtb.SelectionBackColor = Color.Cyan
          index = rtb.Text.IndexOf(searchtext, index) + 1
        End While
      End Sub
    
    '============USAGE============
    
     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
     'this method with search for text "boy" in RichTextBox2, change the textcolor to red and back color to cyan as indicated in the screenshot below 
     findTextAndHighlight("boy", RichTextBox2)   
     End Sub
    

     

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Tuesday, December 07, 2010 10:47 PM

All replies

  • Put your code inside while loop and use selectionBackColor property of richtextbox to highlight the text. Also, remove focus() method.

    Also, in your code, you declared Where as string but passing integer value to it

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Tuesday, December 07, 2010 2:30 AM
  • ok thanks, but lets say i got a "next" button that will search for the next one
    If you don´t Believe in it, Then it Doesn´t Exist!
    Tuesday, December 07, 2010 4:25 PM
  • ok thanks, but lets say i got a "next" button that will search for the next one
    If you don´t Believe in it, Then it Doesn´t Exist!


    Then search richtextbox text line by line

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Tuesday, December 07, 2010 4:56 PM
  • yes but that was my qustion how. with the method mentioned above only search for the first one. ive tried to use richtext.find and do also just find the first word. if my sentence goes like this: the boys in the park. if i want to search fo "the" it will only find the first "the"

     

    any help tahn kyou?


    If you don´t Believe in it, Then it Doesn´t Exist!
    Tuesday, December 07, 2010 6:01 PM
  • yes but that was my qustion how. with the method mentioned above only search for the first one. ive tried to use richtext.find and do also just find the first word. if my sentence goes like this: the boys in the park. if i want to search fo "the" it will only find the first "the"

     

    any help tahn kyou?


    If you don´t Believe in it, Then it Doesn´t Exist!


    In my first post, i told you to put the code that work for single text inside a while loop to search for every single item in richtextbox.

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Tuesday, December 07, 2010 7:14 PM
  • i dont get it to work :( sadly just find the first one ;(
    If you don´t Believe in it, Then it Doesn´t Exist!
    Tuesday, December 07, 2010 7:28 PM
  • i dont get it to work :( sadly just find the first one ;(
    If you don´t Believe in it, Then it Doesn´t Exist!


    Ok, post your code that does not work here, maybe i can fix it for you

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Tuesday, December 07, 2010 8:46 PM
  • WEll i messed it all up and i am completley lost right now, so i have to start it all over :(
    If you don´t Believe in it, Then it Doesn´t Exist!
    Tuesday, December 07, 2010 9:03 PM
  • well, im going to post the code to highlighting all the text you search for at the same time but if that is not what you want, you have to solve it on your own.

    Sub findTextAndHighlight(ByVal searchtext As String, ByVal rtb As RichTextBox)
        Dim textEnd As Integer = rtb.TextLength
        Dim index As Integer = 0
        Dim fnt As Font = New Font(rtb.Font, FontStyle.Bold)
        Dim lastIndex As Integer = rtb.Text.LastIndexOf(searchtext)
        While (index < lastIndex)
          rtb.Find(searchtext, index, textEnd, RichTextBoxFinds.WholeWord)
          rtb.SelectionFont = fnt
          rtb.SelectionLength = searchtext.Length
          rtb.SelectionColor = Color.Red
          rtb.SelectionBackColor = Color.Cyan
          index = rtb.Text.IndexOf(searchtext, index) + 1
        End While
      End Sub
    
    '============USAGE============
    
     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
     'this method with search for text "boy" in RichTextBox2, change the textcolor to red and back color to cyan as indicated in the screenshot below 
     findTextAndHighlight("boy", RichTextBox2)   
     End Sub
    

     

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Tuesday, December 07, 2010 10:47 PM
  • Ok thank you! but if i have multiple words i want to hightlight and maby have different color. do i have to make more than one of these functions? like i want to highlight every boy and every girl?
    If you don´t Believe in it, Then it Doesn´t Exist!
    Wednesday, December 08, 2010 3:52 PM
  • Ok thank you! but if i have multiple words i want to hightlight and maby have different color. do i have to make more than one of these functions? like i want to highlight every boy and every girl?
    If you don´t Believe in it, Then it Doesn´t Exist!


    Well, this will be the last code i will post for you on this issue, you should be able to adapt the code to your own suit. To search multiple text, just put your text in array of string, it can be collection in listbox, combobox, arraylist or generic list. Below is sample code

    Private allSearchText() As String = {"boy", "girl", "and"} 'list of text to search 
     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
     For Each st As String In allSearchText
      findTextAndHighlight(st, RichTextBox2)
      Next
     End Sub
    

    kaymaf 

     


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Wednesday, December 08, 2010 5:16 PM