【问题标题】:How to print color text如何打印彩色文本
【发布时间】:2023-04-06 10:57:01
【问题描述】:
if (counter == 0)
     textBox2.Text += "ERROR: the item in the  " + j + "th row is wrong" + Environment.NewLine;

当 j 变量 Red 出现在 TextBox 中时如何打印它?

【问题讨论】:

标签: c# colors


【解决方案1】:

您可以更改整个字体的颜色。使用textBox2.ForeColor = Color.Red

或使用RichTextBox

【讨论】:

    【解决方案2】:

    首先,将其设为富文本框,而不是常规文本框。

    然后

    Font fnt=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);
    string mystring=@" " + j + "th";
    if (richTextBox1.Find(mystring)>0)
    {
        int my1stPosition=richTextBox1.Find(mystring);
        richTextBox1.SelectionStart=my1stPosition;
        richTextBox1.SelectionLength=mystring.Length; 
        richTextBox1.SelectionFont=fnt;
        richTextBox1.SelectionColor=Color.CadetBlue;
    }
    

    【讨论】:

    • 嗯,也许您确实修复了搜索字符串 (mystring)。或者也许不是
    • 我做到了string mystring=@""+j + 1+"th"; 对吗?
    • @georgemano:我无法从你提供的代码中看出。只需准确地选择您想要着色的文本。你最清楚它是什么文本。如果数字作为其他单词的一部分出现在其他地方,则在左/右包含空格可防止意外替换。
    【解决方案3】:

    不能使用标准的TextBox,您需要使用RichTextbox

    那么,看看如何highlight words in a RichTextBox

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-22
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 2016-03-16
      • 2017-07-17
      相关资源
      最近更新 更多