private void ColorRichTextbox(RichTextBox box, string text, Color color)
{
int length = MyRichTextBox.TextLength;
if (!string.IsNullOrWhiteSpace(MyRichTextBox.Text))
{
MyRichTextBox.AppendText("\r\n" text);
}
else
{
MyRichTextBox.AppendText(text);
}
MyRichTextBox.SelectionStart = length;
MyRichTextBox.SelectionLength = text.Length;
MyRichTextBox.SelectionColor = color;
}
用法:
ColorRichTextbox(MyRichTextBox,
"This is example : " DateTime.Now, Color.Red);
結果是一行紅色的,由“這是示例:”和DateTime.Now零件組成。有沒有辦法制作例如“這是示例:”部分為白色并DateTime.Now為紅色?
uj5u.com熱心網友回復:
您也可以使用自定義的插值字串處理程式來執行此操作。
richTextBox1.AppendColoredLine($"This is an example : {DateTime.Now:red}");
richTextBox1.AppendColoredLine($"This is an example : {DateTime.Now:green}");
richTextBox1.AppendColoredLine($"This is an example : {DateTime.Now:blue}");
richTextBox1.AppendColoredLine($"This is an example : {DateTime.Now:yellow}");
richTextBox1.AppendColoredLine($"Some other colored things: {"hi":orange}, {10:purple}, {this.Name:gray}");

這是使其作業的代碼(需要.Net 6):
using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace YourNamespace;
public static class RichTextBoxExtensions
{
[InterpolatedStringHandler]
[StructLayout(LayoutKind.Auto)]
public ref struct ColoredStringHandler
{
private readonly RichTextBox textBox;
private readonly IFormatProvider? formatProvider;
public ColoredStringHandler(int literalLength, int formattedCount, RichTextBox textBox, IFormatProvider? formatProvider = null)
{
this.textBox = textBox;
this.formatProvider = formatProvider;
textBox.DeselectAll();
}
public void AppendLiteral(string s) => textBox.AppendText(s);
public void AppendFormatted<T>(T item) where T : notnull => AppendLiteral(item.ToString() ?? "");
public void AppendFormatted<T>(T item, string format) where T : notnull
{
var color = Color.FromName(format);
string? s =
item is IFormattable
? ((IFormattable)(object)item).ToString(null, formatProvider)
: item.ToString();
s ??= "";
Color previousColor = textBox.SelectionColor;
try
{
textBox.SelectionColor = color;
textBox.AppendText(s);
}
finally
{
textBox.SelectionColor = previousColor;
}
}
}
public static void AppendColored(this RichTextBox textBox, IFormatProvider? formatProvider,
[InterpolatedStringHandlerArgument("textBox", "formatProvider")] ref ColoredStringHandler handler)
{
}
public static void AppendColored(this RichTextBox textBox,
[InterpolatedStringHandlerArgument("textBox")] ref ColoredStringHandler handler)
{
}
public static void AppendColoredLine(this RichTextBox textBox, IFormatProvider? formatProvider,
[InterpolatedStringHandlerArgument("textBox", "formatProvider")] ref ColoredStringHandler handler)
{
textBox.AppendText(Environment.NewLine);
}
public static void AppendColoredLine(this RichTextBox textBox,
[InterpolatedStringHandlerArgument("textBox")] ref ColoredStringHandler handler)
{
textBox.AppendText(Environment.NewLine);
}
}
uj5u.com熱心網友回復:
如果您修改您的方法以不總是將文本強制放在自己的行上,您可以呼叫它兩次 - 首先是白色文本,然后是紅色:
private void ColorRichTextbox(RichTextBox box, string text, Color color, bool appendNewLine = true)
{
int length = box.TextLength;
if (!string.IsNullOrWhiteSpace(box.Text) && appendNewLine)
{
box.AppendText("\r\n" text);
}
else
{
box.AppendText(text);
}
box.SelectionStart = length;
box.SelectionLength = text.Length;
box.SelectionColor = color;
}
通過設定appendNewLine為false,輸入的文本將不會放在單獨的行上,因此會從上一個呼叫停止的地方繼續:
ColorRichTextbox(MyRichTextBox, "This is example : ", Color.White);
ColorRichTextbox(MyRichTextBox, DateTime.Now.ToString(), Color.Red, false); // false = Don't write this text on a separate line
旁注:您似乎沒有使用box您的方法的引數。如果您想將它用于多個 RichTextBoxes,您應該在您的方法中替換MyRichTextBox為box(這已在我上面的代碼中完成)。
結果:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465198.html
下一篇:ComboBox.SelectedValue.ToString()拋出NullReferenceException

