c#富文本框着色和粗体

时间:2013-10-27 18:05:03

标签: c# colors richtextbox bold

我是C#的新手但是,我希望自己成为dbg格式的调试控制台。 我想要为它添加变量的颜色和粗体,我创建了一个易于写入控制台的功能:

    private void writtodbg(string x, string y)
    {
        string a = Convert.ToString(x);
        string b = Convert.ToString(y);

        Debug.rTB1.AppendText(a, Color.Orange); // bold
        Debug.rTB1.AppendText(" = "); // bold
        Debug.rTB1.AppendText(b + Environment.NewLine, Color.Orange); // bold

    }

然后发生错误,说“方法'没有重载'AppendText'需要2个参数”。

1 个答案:

答案 0 :(得分:3)

那是因为AppendText()只能接收一个String。您无法指定颜色。如果您看到来自某个地方的代码具有类似的语法,那么它可能是一个自定义的RichTextBox类,其中有人添加了该功能。

尝试这样的事情:

    private void writtodbg(string x, string y)
    {
        AppendText(x, Color.Orange, true);
        AppendText(" = ", Color.Black, false);
        AppendText(y + Environment.NewLine, Color.Orange, true);
    }

    private void AppendText(string text, Color color, bool bold)
    {
        Debug.rTB1.SelectionStart = Debug.rTB1.TextLength;
        Debug.rTB1.SelectionColor = color;
        Debug.rTB1.SelectionFont = new Font(Debug.rTB1.Font, bold ? FontStyle.Bold : FontStyle.Regular);
        Debug.rTB1.SelectedText = text;
    }
相关问题