C# 文本框文本颜色更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14085607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
textbox text color change
提问by Andrii
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?
我有textbox
textbox1
并且我想更改文本颜色,但在所有文本的一部分。例如从/*
到*/
喜欢 Visual Studio 中的评论?
How I can do this?
我怎么能做到这一点?
采纳答案by Andrii
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:
和 MSDN:
http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.document.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.document.aspx
回答by Andrii
You can do this, however, you may want to look into the RichTextBox control where is it much easier to do.
您可以这样做,但是,您可能需要查看 RichTextBox 控件在哪里更容易做到。
Simple example:
简单的例子:
richtextbox.SelectionFont = new Font("Verdana", 10, FontStyle.Regular);
richtextbox.SelectionColor = Color.Blue;
回答by ChrisF
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.
您必须从TextBox
允许用户更改颜色或根据您的规则更改颜色的代码中派生出一个控件并放入其中。
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:
ARichTextBox
将为您提供此基础,因为它允许不同的文本“运行”,每个文本都可以具有自己的样式:
<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.
如果您添加颜色等控件,那么您可以从用户选择的所需样式中创建一个新的运行。