C#Windowsフォームを使用していて、richtextboxがあり、一部のテキストを赤、一部を緑、一部を黒で色付けしたいと考えています。
そうする方法は?画像添付。
C#Windowsフォームを使用していて、richtextboxがあり、一部のテキストを赤、一部を緑、一部を黒で色付けしたいと考えています。
そうする方法は?画像添付。
System.Windows.Forms.RichTextBox
現在の選択または挿入ポイントのテキストの色を取得または設定するColor
名前のタイプのプロパティを持っています。SelectionColor
このプロパティを使用してRichTextBox
、指定した色での特定のフィールドをマークできます。
例
RichTextBox _RichTextBox = new RichTextBox(); //Initialize a new RichTextBox of name _RichTextBox
_RichTextBox.Select(0, 8); //Select text within 0 and 8
_RichTextBox.SelectionColor = Color.Red; //Set the selected text color to Red
_RichTextBox.Select(8, 16); //Select text within 8 and 16
_RichTextBox.SelectionColor = Color.Green; //Set the selected text color to Green
_RichTextBox.Select(0,0); //Select text within 0 and 0
注意:値を与える際に内のテキストを強調表示したい場合は、これを使用して計算を回避RichTextBox.Find(string str)
できます。Object Browser
Lines
RichTextBox
例
RichTextBox _RichTextBox = new RichTextBox(); //Initialize a new RichTextBox of name _RichTextBox
_RichTextBox.Find("Account 12345, deposit 100$, balance 200$"); //Find the text provided
_RichTextBox.SelectionColor = Color.Green; //Set the selected text color to Green
ありがとう、
これがお役に立てば幸いです:)
文字列の色を変更したり、改行値を挿入したりできるこの拡張メソッドを見つけました。
public static void AppendText(this RichTextBox box, string text, Color color, bool AddNewLine = false)
{
if (AddNewLine)
{
text += Environment.NewLine;
}
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
Runオブジェクトを使用して、実行時に色を変更できます
private Run GetForegroundColor(string strInformation, Brush color)
{
Run noramlRun = new Run(strInformation);
noramlRun.Foreground = color;
return noramlRun;
}
要件に基づいて色を変更するなどのより複雑なシナリオについては、ブローリンクにアクセスしてください