循环更改富文本框字体颜色
这似乎对我有用,但我似乎无法弄清楚
public void ShowReport()
{
foreach (KeyValuePair<int, ReportSet> pair in ReportSets)
{
ReportText.Text += pair.Value.ReportSetText + Environment.NewLine;
foreach (string message in pair.Value.ReportMessages)
{
ReportText.Text += message;
ReportText.Select(ReportText.Text.LastIndexOf(message), message.Length);
ReportText.SelectionColor = pair.Value.Color;
}
ReportText.Text += Environment.NewLine;
}
this.Show();
}
按原样,这不会改变文本颜色。如果我在内循环之后删除新行,它将仅更改最后一条消息的颜色。尝试仅删除 s 和 g 的所有新行,但结果相同。有什么想法吗?
This seems like it should work to me but I can't seem to figure it out
public void ShowReport()
{
foreach (KeyValuePair<int, ReportSet> pair in ReportSets)
{
ReportText.Text += pair.Value.ReportSetText + Environment.NewLine;
foreach (string message in pair.Value.ReportMessages)
{
ReportText.Text += message;
ReportText.Select(ReportText.Text.LastIndexOf(message), message.Length);
ReportText.SelectionColor = pair.Value.Color;
}
ReportText.Text += Environment.NewLine;
}
this.Show();
}
As is, this changes no text color. If I remove the new line after the inner loop, it will change the color of the last message only. Tried removing all of the new lines just for s's and g's but the same result. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用 AppendText() 而不是
+=
添加文本,并避免使用LastIndexOf()
来计算您的选择范围。着色后清除选择也可能是一个好主意:I'd suggest you use AppendText() instead of
+=
to add your text, and avoid usingLastIndexOf()
to compute your selection bounds. Clearing the selection after coloring it might also be a good idea: