15

C#Windowsフォームを使用していて、richtextboxがあり、一部のテキストを赤、一部を緑、一部を黒で色付けしたいと考えています。

そうする方法は?画像添付。

ここに画像の説明を入力してください

4

3 に答える 3

33

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 BrowserLinesRichTextBox

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

ありがとう、
これがお役に立てば幸いです:)

于 2012-11-04T17:21:09.887 に答える
12

文字列の色を変更したり、改行値を挿入したりできるこの拡張メソッドを見つけました。

    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;
    }
于 2013-03-20T19:10:09.920 に答える
-1

Runオブジェクトを使用して、実行時に色を変更できます

private Run GetForegroundColor(string strInformation, Brush color)
        {
            Run noramlRun = new Run(strInformation);
            noramlRun.Foreground = color;
            return noramlRun;
        }

要件に基づいて色を変更するなどのより複雑なシナリオについては、ブローリンクにアクセスしてください

https://sites.google.com/site/greateindiaclub/mobil-apps/windows8/customwpfrichtextboxwithcolorchangeandhighlightfunctionality

于 2014-11-26T12:42:10.840 に答える