tags:

views:

251

answers:

3

Hi folks,

is there an easy possibility to highlight a portion of text in a normal WinForms TextBox (I cannot use RichTextBox in this case). All the solutions I came up with so far are very complex and handle drawing the text on their own, including fancy Interop calls...

Thanks in advance!

EDIT: I don't talk about selecting the text but highlighting parts of it with a background color or a colored underline. Thanks again

+1  A: 

If you mean to change the color or font style of part of the text in a regular TextBox control, there is no support for that. What you can do is to select a portion of the text to make it stand out, but that is obviously a very temporary solution (note that the HideSelection property must be set to false for this to show when the TextBox does not have focus):

// select the 8 characters, starting after the fifth character
myTextBox.Select(5, 8);
Fredrik Mörk
Thanks, thats what I found out, too
m0rb
A: 

Set the following properties

TextBox1.SelectionStart = 10
TextBox1.SelectionLength = 8
A: 

I finally ended up implementing the behavior on my own. http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox/ was really helpful.

m0rb