0

Greetings!
I just want to ask on how to change a word to another word and change its color in a textbox. For example if I type the word "Yes" in the text box it will change to "NO" and change its color into red at the same time.

3
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by deceptikon
2

You can do a simple replace on the string:

textBox.Text = textBox.Text.Replace("Yes", "NO")

Changing the color is a different matter. I assume you want to change only the replaced text to red, which won't work unless you're using a rich text box. If it's just a regular text box then you're limited to a global foreground color change. But that can be done by changing the ForeColor property:

textBox.ForeColor = Color.Red

For a rich text box it's a bit more complicated, but you can search for substrings with that control, select them, and then set the color for the selection to red. See the documentation for details.

-1

Sir deceptikon how about if I use rich text box what is the code for changing the color?

1

how about this

if RichTextBox1.Text = "yes" Then
    RichTextBox1.Select(RichTextBox1.TextLength - 3, 3)
    RichTextBox1.SelectionColor = Color.Red
    RichTextBox1.SelectedText = "no"
End if
0

Sir deceptikon how about if I use rich text box what is the code for changing the color?

I especially like how you read the documentation I linked you to that gave details on how to do exactly that before asking. Oh wait, you didn't. :rolleyes:

Edited by deceptikon

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.