在 C# Forms 中格式化文本颜色
Format text color in C# Forms
配置:
- Windows 7
- .NET 3.5
- Visual Studio 2008
总结:通过for循环将单词发送到rtb;根据内容格式化它们("OK" 显示为绿色,"failed" 显示为红色)。
代码:
for (int i = 0; i < inputValues.Length; i++)
{
//checking if the value is OK or not
string answer = functionReturningString(inputValues[i], referenceValue);
textBox4.Text += answer; //and sending the result string to text box
}
现在我只是尝试 select 最后添加的字符串并根据其内容对其进行格式化。
textBox4.SelectAll();
textBox4.Select(textBox4.SelectedText.Length - answer.Length, answer.Length);
if (answer == "OK")
{
textBox4.SelectionColor = Color.Green;
} else {
textBox4.SelectionColor = Color.Red;
}
textBox4.Refresh();//I want to see every value as soon as added
textBox4.Text += "\r\n"; //adding space between words
至于结果,它最终会在 rtb 中的所有单词上使用 "SelectionColor"。
问:如何确保之前格式化过的文字不再改变颜色?
更新: 建议的解决方案也不起作用。单词将以正确的颜色显示(首先)。然后添加下一个单词,整个框的颜色发生变化。
顺序应该是这样的(假设您从空的富文本框开始):
richTextBox.SelectionColor = some_Color;
richTextBox.AppendText(some_Text);
这里是模拟你描述的案例的例子(如果我理解正确的话):
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace Tests
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form();
var richTextBox = new RichTextBox { Dock = DockStyle.Fill, Parent = form };
var button = new Button { Dock = DockStyle.Bottom, Parent = form, Text = "Test" };
button.Click += (sender, e) =>
{
Color TextColor = Color.Black, OKColor = Color.Green, FailedColor = Color.Red;
var questions = Enumerable.Range(1, 20).Select(n => "Question #" + n).ToArray();
var random = new Random();
richTextBox.Clear();
for (int i = 0; i < questions.Length; i++)
{
richTextBox.SelectionColor = TextColor;
richTextBox.AppendText(questions[i] + ":");
bool ok = (random.Next() & 1) != 0;
richTextBox.SelectionColor = ok ? OKColor : FailedColor;
richTextBox.AppendText(ok ? "OK" : "Failed");
richTextBox.SelectionColor = TextColor;
richTextBox.AppendText("\r\n");
}
};
Application.Run(form);
}
}
}
产生以下
配置:
- Windows 7
- .NET 3.5
- Visual Studio 2008
总结:通过for循环将单词发送到rtb;根据内容格式化它们("OK" 显示为绿色,"failed" 显示为红色)。
代码:
for (int i = 0; i < inputValues.Length; i++)
{
//checking if the value is OK or not
string answer = functionReturningString(inputValues[i], referenceValue);
textBox4.Text += answer; //and sending the result string to text box
}
现在我只是尝试 select 最后添加的字符串并根据其内容对其进行格式化。
textBox4.SelectAll();
textBox4.Select(textBox4.SelectedText.Length - answer.Length, answer.Length);
if (answer == "OK")
{
textBox4.SelectionColor = Color.Green;
} else {
textBox4.SelectionColor = Color.Red;
}
textBox4.Refresh();//I want to see every value as soon as added
textBox4.Text += "\r\n"; //adding space between words
至于结果,它最终会在 rtb 中的所有单词上使用 "SelectionColor"。
问:如何确保之前格式化过的文字不再改变颜色?
更新: 建议的解决方案也不起作用。单词将以正确的颜色显示(首先)。然后添加下一个单词,整个框的颜色发生变化。
顺序应该是这样的(假设您从空的富文本框开始):
richTextBox.SelectionColor = some_Color;
richTextBox.AppendText(some_Text);
这里是模拟你描述的案例的例子(如果我理解正确的话):
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace Tests
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form();
var richTextBox = new RichTextBox { Dock = DockStyle.Fill, Parent = form };
var button = new Button { Dock = DockStyle.Bottom, Parent = form, Text = "Test" };
button.Click += (sender, e) =>
{
Color TextColor = Color.Black, OKColor = Color.Green, FailedColor = Color.Red;
var questions = Enumerable.Range(1, 20).Select(n => "Question #" + n).ToArray();
var random = new Random();
richTextBox.Clear();
for (int i = 0; i < questions.Length; i++)
{
richTextBox.SelectionColor = TextColor;
richTextBox.AppendText(questions[i] + ":");
bool ok = (random.Next() & 1) != 0;
richTextBox.SelectionColor = ok ? OKColor : FailedColor;
richTextBox.AppendText(ok ? "OK" : "Failed");
richTextBox.SelectionColor = TextColor;
richTextBox.AppendText("\r\n");
}
};
Application.Run(form);
}
}
}
产生以下