Change background color for text

E

Einar Høst

Hi,

I'm wondering how I can get a span-tag style effect in a text box in my
windows forms app. In particular, I would like to be able to change the
background color for a text that doesn't match a certain regular expression.
In HTML, I would have done something like:

I'm writing almost <span style="...">erorr</span>-free text here.

I've been screening through documentation all day looking for a clue, but
haven't found any... Any help would be much appreciated.
 
D

Derek Harmon

Einar Høst said:
in a text box in my
windows forms app. In particular, I would like to be able to change the
background color for a text that doesn't match a certain regular expression.

System.Windows.Forms.TextBox has very limited support along these
lines. The only thing I think you can do is set a SelectionStart and
SelectionLength, which will "highlight" ONE range of text.

For instance, if TextBox1.Text were "Hello World!" and you set

TextBox1.SelectionStart = 6;
TextBox1.SelectionLength = 5;

Then the word "World" (but not "Hello" or the exclamation point, '!')
is highlighted in the system's defined KnownColor.HighlightText.

For what you want to do, you'll probably require RichTextBox. It
also only selects one range of text, but you can traverse the text
contents and apply a background color to each selected range.


Derek Harmon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top