【问题标题】:c# Rich text box coloring and boldingc#富文本框着色和加粗
【发布时间】:2013-11-06 10:31:30
【问题描述】:

我是 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 个参数”。

【问题讨论】:

标签: c# colors richtextbox bold


【解决方案1】:

那是因为 AppendText() 只能接收一个字符串。您不能指定颜色。如果您从网上某处看到具有类似语法的代码,那么它可能是一个自定义 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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多