在 C# 中,如何更改文本框中新添加文本的颜色 [英] In C#, how to change colour of newly added text in Text box
问题描述
考虑我使用 C# VS 2005 构建的 Windows 应用程序在多行文本框中显示某些内容.现在,当我添加一些新文本时,它应该以不同的颜色显示.即我需要区分我的项目运行时显示的文本和我在文本框中输入的文本.我该怎么做.?
Consider my windows application built using C# VS 2005 displays certain contents in a multi lined text box. Now when i add some new text, it should appear in a different colour. i.e i need to differentiate the text which gets displayed when my project is run and the text that i enter in the text box. how can i do this.?
推荐答案
普通 TextBox
控件不支持多种颜色、字体等.
The plain TextBox
control doesn't support multiple colours, fonts etc.
我怀疑您需要使用 RichTextBox
代替.然后您可以设置 SelectionColor
属性来改变当前选中文本或插入到当前插入点的文本的颜色.
I suspect you'll need to use a RichTextBox
instead. You can then set the SelectionColor
property to change the colour of the currently selected text or text inserted at the current insertion point.
我应该补充一点,经过几次快速实验后,我发现设置选择颜色然后立即以编程方式附加文本并不像我希望的那么简单.也许另一个答案会解释原因:) 但是,用户输入的文本肯定会立即以新颜色显示...
I should add that after a few quick experiments, I've found that setting the selection colour and then immediately appending text programmatically doesn't work quite as straightforwardly as I'd hoped. Maybe another answer will explain why :) However, text entered by the user certainly appears in the new colour immediately...
这篇关于在 C# 中,如何更改文本框中新添加文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!