8

I have textbox textbox1 and I want to change text color, but in the part of all text. For example from /* to */ like comments in visual studio?

How I can do this?

Andrii
  • 131
  • 1
  • 2
  • 7
  • 1
    [WPF RichTextBox](http://stackoverflow.com/questions/5442067/change-color-and-font-for-some-part-of-text-in-wpf-c-sharp) – Ofiris Dec 29 '12 at 20:26
  • Wpf TextBox not RichTextBox – Andrii Dec 29 '12 at 20:36
  • @Andrii - Use the link that Ofiris provided then - that should work for you. –  Dec 29 '12 at 20:44
  • Here is an example with a Fontdialog and Colordialog. [http://stackoverflow.com/a/32036417/5230895][1] [1]: http://stackoverflow.com/a/32036417/5230895 – berta Aug 16 '15 at 15:00

3 Answers3

6

Try this one:

TextRange rangeOfText1 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);
rangeOfText1.Text = "Text1 ";
rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

TextRange rangeOfWord = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);
rangeOfWord.Text = "word ";
rangeOfWord.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
rangeOfWord.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular);

TextRange rangeOfText2 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);
rangeOfText2.Text = "Text2 ";
rangeOfText2.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
rangeOfText2.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

or this:

public TestWindow()
{
InitializeComponent();

this.paragraph = new Paragraph();
rich1.Document = new FlowDocument(paragraph);

var from = "user1";
var text = "chat message goes here";
paragraph.Inlines.Add(new Bold(new Run(from + ": "))
{
    Foreground = Brushes.Red
});
paragraph.Inlines.Add(text);
paragraph.Inlines.Add(new LineBreak());
this.DataContext = this;
}
private Paragraph paragraph;

Source:

Change color and font for some part of text in WPF C#

And MSDN:

http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.document.aspx

Community
  • 1
  • 1
0

You can do this, however, you may want to look into the RichTextBox control where is it much easier to do.

Simple example:

richtextbox.SelectionFont = new Font("Verdana", 10, FontStyle.Regular);
richtextbox.SelectionColor = Color.Blue;
  • Don't you mean "you can't do this"? – ChrisF Dec 29 '12 at 20:30
  • @ChrisF - You *can*. Not sure how "pure" this is: http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox/(http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox/) –  Dec 29 '12 at 20:34
  • 1
    It's not a `TextBox` but a control derived from a `TextBox` in much the same way that a `RichTextBox` is. – ChrisF Dec 29 '12 at 20:36
  • Ok. And how I can do this in RichTextBox? – Andrii Dec 29 '12 at 20:39
0

You will have to derive a control from TextBox and put in code that will either allow the user to change the colour or changes the colour based on your rules.

A RichTextBox will give you the basis for this as it allows different "runs" of text each of which can have it's own styling:

<RichTextBox Name="richTB">
  <FlowDocument>
    <Paragraph>
      <Run>Paragraph 1</Run>
    </Paragraph>
    <Paragraph>
      <Run>Paragraph 2</Run>
    </Paragraph>
    <Paragraph>
      <Run>Paragraph 3</Run>
    </Paragraph>
  </FlowDocument>
</RichTextBox>

If you add controls for colour etc. then you can create a new run from the user's selection with the required style.

ChrisF
  • 134,786
  • 31
  • 255
  • 325