Answered Take Action When a Word is Typed

  • Sunday, April 01, 2012 4:07 AM
     
     
    What is a code that I can use for my program to do something when the user types a certain word into a rich text box?

    Bgeo99

All Replies

  • Sunday, April 01, 2012 7:19 AM
     
     Proposed Has Code
    You would put code in the TextChanged event of the RTB and look at the text to see if the word had been entered.
        Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
            If RichTextBox1.Text.EndsWith("Microsoft") Then
                MsgBox("No swearing please")
            End If
        End Sub
  • Sunday, April 01, 2012 1:03 PM
     
      Has Code

    I tried this but it doesn't work. It just types another "Echo" word and gets a bit glitchy.

        Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
            If RichTextBox1.Text.EndsWith("Echo") Then
                Dim Echo As String = "Echo"
                RichTextBox1.SelectedText = Echo
                RichTextBox1.SelectionColor = Color.Blue
                RichTextBox1.SelectionFont = New Font("Courier New", 11, FontStyle.Bold)
                RichTextBox1.DeselectAll()
            End If
        End Sub
    Is there another way to make the word "Echo" from my code the color blue and bold so I can avoid the problems I've been having?

    Bgeo99

  • Sunday, April 01, 2012 4:21 PM
    Moderator
     
     Answered Has Code

    You have to select the existing text and reformat it.  When you set the selected text like that, it is empty to start with so you are "inserting" new text.  Instead, position the selection start and selection length then make your changes to the formatting.  Something like:

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        If RichTextBox1.Text.EndsWith("Echo") Then
            RichTextBox1.SelectionStart -= 4
            RichTextBox1.SelectionLength = 4
            RichTextBox1.SelectionColor = Color.Blue
            RichTextBox1.SelectionFont = New Font("Courier New", 11, FontStyle.Bold)
            RichTextBox1.SelectionStart = RichTextBox1.TextLength
            RichTextBox1.SelectionColor = RichTextBox1.ForeColor
            RichTextBox1.SelectionFont = RichTextBox1.Font
        End If
    End Sub


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


  • Sunday, April 01, 2012 5:05 PM
     
     

    Hi,

    Check for the Text changed event and check for the space key pressed to get the word.

  • Sunday, April 01, 2012 7:35 PM
     
     

    One last thing Reed Kimble, how can I make it so it undoes the blue and bold for "Echo" when I press backspace and it becomes "Ech", because normally it just stays blue and bold...


    Bgeo99

  • Sunday, April 01, 2012 9:00 PM
     
     

    That depends.  Are there other words in the text that you have highlighted?  If so, should they remain highlighted when the user deletes the 'o', or should all the highlighting disappear if the last word entered doesn't match, and only the last word matched should be highlighted again? 

    The point is important, because if you have multiple highlighted words and if you want the highlighting removed when the user makes edits (and backspace is not the only edit that could occur) then you would not use the solution I proposed.  Your original intention was "do something when the user types a certain word".  That's a relatively simple task.  It now seems that your task has become much more complex, and you may need to rethink the way you tckle it.

    If you only have one highlighted word and you want the highlighting removed when the user types backspace to delete any part of that word, then you can simply use a variation of the code above to make all the text selected text and remove all highlighting before selecting the last word (if it matches) and highlighting that word.

  • Sunday, April 01, 2012 9:17 PM
     
     

    Tell me which code to use if let's say I type one 'Echo' and then another 'Echo' on a new line, or the same line (I don't know if it matters), and I delete any part of the word (either the 1st 'Echo' or the 2nd) and it automatically undoes the blue and bold. Likewise, if I rewrite the word, it will redo the blue and bold.

    Basically what I mean is that I need a code that works for any way the word is changed, whether 'Ech' or 'E' or 'Eho', etc., to change to undo the blue and bold.

    What code is best for this?


    Bgeo99

  • Sunday, April 01, 2012 9:38 PM
     
     

    This is now quite a different question.  If you want the word to be highligted wherever it appears in the text, and if you want to allow any editing of the text, then you need to examine the whole text for all occurrences of the word (not just EndingWith) and hghlight each occurrence as it is detected.

    That will require a loop to search the text for the word, highlighting as you go.

    You haven't indicated whether you want to highlight just one word or multiple words - this example assumes just one word. 

            Dim strSearch As String = "echo"
            Dim index As Integer = 0
            While index <> -1
                index = RichTextBox1.Find(strSearch, index, 0)
                If index <> -1 Then
                    RichTextBox1.SelectionStart = index
                    RichTextBox1.SelectionLength = strSearch.Length
                    RichTextBox1.SelectionBackColor = Color.Yellow
                    index += 1
                End If
            End While
            RichTextBox1.SelectionStart = RichTextBox1.TextLength
            RichTextBox1
    .SelectionBackColor = RichTextBox1.BackColor

  • Sunday, April 01, 2012 9:52 PM
     
     

    Where should I place this? I placed it in the same sub as Reed Kimble's code and it didn't work. What is the code for multiple words?

    I actually didn't want to highlight anything. I just wanted any 'Echo''s to be made blue and bold but I can change that, thanks. What I was aiming for is that if any part of any occurrence of the word 'Echo' in the rich text box was deleted and left only half of the word, that only word would change back to black and regular instead of bold. If the word is made back into 'Echo', it would be recolored and bolded again.


    Bgeo99



    • Edited by Bgeo99 Sunday, April 01, 2012 9:53 PM
    •  
  • Monday, April 02, 2012 3:17 AM
    Moderator
     
     

    As Acamar said, this has become a whole different question... you may need to start a new thread because this current approach is not likely to work well for you.

    The example Acamar gave would have to go in your TextChanged event handler after the code you already have.  However, you can see now that this loop would run after every key press and could quickly become a bottleneck with much text in the textbox.

    You could place it in a key-up event handler and only execute it if the key is backspace or delete, but you could still have an issue if the text is very long.

    This could be a difficult objective to achieve... you might want to start a new thread with a detailed explanation of what the program is supposed to do.  You may have to rely on a custom textbox of some sort (either 3rd party which someone recommends, or something you develop - but be aware that developing a custom textbox would be no simple task).


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

  • Monday, April 02, 2012 5:47 AM
     
     

    What do you mean 'didn't work'?  Was there an error?  If so, what was the error and what line did it occur at?  If there wasn't an error then do you mean that the code did something different htan what you expected?  If so, what did you exepect to happen and what actually happened?  The information you have provided is not a lot to go on.

    To make this code work for multiple words you would put it in a loop that repeats for each word, updating the strSearch variable for the new word each time through the loop.