Reputation: 6480
Is it possible to have differently colored lines in multiline text box?
I'm adding data to the text box and I want to clarify for the user different types of text by color.
How to do this if it's possible?
Upvotes: 9
Views: 15877
Reputation: 11
I find a way without using the RichTextBox
WrapPanel panel = new WrapPanel();
foreach (....)
{
TextBlock tb = new TextBlock();
if(.....)
tb.Foreground = Brushes.Red;
else
{
tb.Foreground = Brushes.LemonChiffon;
panel.Children.Add(tb);
}
and finally
stackPanel.Children.Add(panel);
}
scrollViewer.Content = stackPanel;
scrollViewer.CanContentScroll = true;
window.Content = scrollViewer;
window.Show();
Its not necessary to ust the Scrollviewer. But well in my App i needed it. However like this its possible to generate Multicolor text...and an extra for free. Each part of the text can easiely have a ToolTip
Upvotes: 1
Reputation: 2860
So depending on how you want the output, there's an browser/html control (basically a box), that you can pass a URL or a string of HTML, depending on what you're doing, you might want to use the RickTextBox - which has formatting commands - you could use regular expressions, or the offset of the text and then use a "set color" command on the text box with an IF structure.
Upvotes: 1
Reputation: 129657
You can do this if you use a RichTextBox
control. See the documentation here (particularly look at the Remarks and Examples sections). The standard TextBox does not offer this capability.
Upvotes: 10
Reputation: 18965
The standard Winforms textbox does not have this ability (and adding it would be troublesome).
You could look at using the System.Windows.Forms.RichTextBox
as an alternative for this or one of the many commercial alternatives.
Upvotes: 3