0

What im trying to do is search specific string in textbox and change fore color with red color, and it works so far:

    Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged

    Dim loc As Integer
    'check if it contains the <html> tag and if it does select it and change the colour to red.
    If RichTextBox1.Text.Contains("Card Locked") Then

        loc = RichTextBox1.Find("Card Locked")
        RichTextBox1.Select(loc, 12)
        RichTextBox1.SelectionColor = System.Drawing.Color.Red
        'TextBox1.ForeColor = System.Drawing.Color.Red

    End If
    If RichTextBox1.Text.Contains("Card Unlocked") Then
        loc = TextBox1.Find("Card Unlocked")
        RichTextBox1.Select(loc, 14)
        RichTextBox1.SelectionColor = System.Drawing.Color.Red


    End If 
    End sub

But it changes color only for first serach for exmaple if in richtextbox1 i have two lines with "Card Locked" in diffrent places, it will colored only first one and another one will stay with default color. How to make it search in whole text and chage fore color everytime app founds needed string?

kooda
  • 11
  • 8
  • For one thing, the `TextBox` class has no `Find` or `SelectionColor` members. Presumably you are working with a `RichTextBox`. Please describe your issue accurately to avoid confusion and wasting our time and yours as a result. – jmcilhinney Apr 18 '18 at 06:28
  • As for the question, the `Find` method allows you to specify where in the text to start searching. That means that you can call `Find` in a loop and start each search after the previous match until no match is found. – jmcilhinney Apr 18 '18 at 06:31
  • The looping option that I suggested could be quite time-consuming if there's any reasonable amount of text. You should be a bit smarter than searching on every `TextChanged` event. It shouldn;t be too hard to work out whether the change that just occurred possibly could have caused a new match to have been added or an existing one to have been removed by looking at where the caret is and what characters are around it. If you want to match "XXX" and the caret is in between two "Y" characters then what's the point of searching at all? – jmcilhinney Apr 18 '18 at 06:34
  • yeah sry, im using richtextbox. Btw i undrstands your suggestions but i just cant realize how to make it search till the end of the text, i unsrstand that i can TextBox1.Find("Card Locked", IndexToStart), but how to make it loop properly. The Text is not huge its arond 1000chars – kooda Apr 18 '18 at 06:47
  • As with ALL programming problems, you need to understand the logic BEFORE you can write the code. Pick up a pen and paper and write down the steps that you would need to perform to achieve your aim manually. Only when you have a working algorithm should you try to write code because it's that algorithm that the code needs to implement. To say that you CAN'T do something after possibly trying for no more than 10 minutes is simply not accurate. You would need to try much harder to know that you are unable to do it for yourself. – jmcilhinney Apr 18 '18 at 06:52
  • See if this one can help [Color a Specific Word in every richtextbox line](https://stackoverflow.com/questions/49335419/color-a-specific-word-in-every-richtextbox-line-vb?answertab=active#tab-top) – Jimi Apr 18 '18 at 13:34

2 Answers2

1

Try this, I tried it and it works:

For Each ctrl As Control In Controls
        If TypeOf ctrl Is RichTextBox AndAlso DirectCast(ctrl, RichTextBox).Text.Contains(INSERT_YOUR_CHECK_STRING) Then
            'do what you want'
        End If
Next

Remember to use DirectCast(ctrl, RichTextBox) to refer to the selected checkbox!

Simo
  • 955
  • 8
  • 18
0

Well, i made this, if someoone will need it:

       Dim index As Integer = 0
    While index < TextBox1.Text.LastIndexOf("Card Locked")
        TextBox1.Find("Card Locked", index, TextBox1.TextLength, RichTextBoxFinds.None)
        TextBox1.SelectionColor = System.Drawing.Color.Red
        index = TextBox1.Text.IndexOf("Card Locked", index) + 1
    End While
kooda
  • 11
  • 8