为richTextBox C#中的特定文本行着色

问题描述

我是这里的新手,也是C#的新手。我基本上只想为报告中的最高温度(单行)上色,但是我在这里尝试了所有编码,但没有任何效果。有人可以帮帮我吗?这是我的编码,用于比较两个城市的预测。如果还有其他更简便的方法可以使用listBox等其他功能,请告诉我。

如果看到图片,我现在基本上只是使用星号来显示更高的温度。

输出

New output

我的代码

private void getComparison()
{

    this.richTextBox1.Text = ("Start Date:  " + dateTimePicker3.Value.ToString());
    this.richTextBox1.Text = ("COMPARE MULTIPLE CITIES FOR THE WEEK" + Environment.NewLine + "\n\n");

    for (int j = 0; j < 7; j++)
    {
        tempCity1 = Convert.ToInt32(a.getMaxi()[comboBox2.Selectedindex,j].ToString());
        tempCity2 = Convert.ToInt32(a.getMaxi()[comboBox1.Selectedindex,j].ToString());

        //City 1
        this.richTextBox1.Text = ("------------------------------------------");
        this.richTextBox1.Text = ("Day:  " + a.getDate(j).ToString());
        this.richTextBox1.Text = ("------------------------------------------");
        this.richTextBox1.Text = ("City:  " + arrCities[comboBox2.Selectedindex]);
        this.richTextBox1.Text = ("Minimum Temperature:  " + a.getMini()[comboBox2.Selectedindex,j].ToString() + "°C");
        
        
        
        if (tempCity1 < tempCity2)
        {
            this.richTextBox1.Text =("Maximum Temperature: " + a.getMaxi()[comboBox2.Selectedindex,j].ToString() + "°C");
            //richTextBox1.SelectionColor = Color.Red;
        }
        else
        {
            this.richTextBox1.Text = ("***Maximum Temperature: " + a.getMaxi()[comboBox2.Selectedindex,j].ToString() + "°C");
            //richTextBox1.SelectionBackColor = Color.Blue;
        }

        this.richTextBox1.Text = ("Precipitation:  " + a.getPrecip()[comboBox2.Selectedindex,j].ToString() + "%");
        this.richTextBox1.Text = ("Humidity:  " + a.getHumid()[comboBox2.Selectedindex,j].ToString() + "%");
        this.richTextBox1.Text = ("Wind speed:  " + a.getwindspeed()[comboBox2.Selectedindex,j].ToString() + "km / h");
        this.richTextBox1.Text = ("" + Environment.NewLine + "\n\n");

        //City 2
        this.richTextBox1.Text = ("City:  " + arrCities[comboBox1.Selectedindex]);
        this.richTextBox1.Text = ("Minimum Temperature:  " + a.getMini()[comboBox1.Selectedindex,j].ToString() + "°C");

        if (tempCity1 > tempCity2)
        {
            this.richTextBox1.Text=("Maximum Temperature: " +  a.getMaxi()[comboBox1.Selectedindex,j].ToString() + "°C");
            //richTextBox1.SelectionColor = Color.Red;
        }
        else
        {
            this.richTextBox1.Text = ("**Maximum Temperature:" + a.getMaxi()[comboBox1.Selectedindex,j].ToString() + "°C");
            //richTextBox1.SelectionColor = Color.Blue;
        }


        //("Maximum Temperature:  " + a.getMaxi()[comboBox1.Selectedindex,j].ToString() + "°C");
        this.richTextBox1.Text = ("Precipitation:  " + a.getPrecip()[comboBox1.Selectedindex,j].ToString() + "%");
        this.richTextBox1.Text = ("Humidity:  " + a.getHumid()[comboBox1.Selectedindex,j].ToString() + "%");
        this.richTextBox1.Text = ("Wind speed:  " + a.getwindspeed()[comboBox1.Selectedindex,j].ToString() + "km / h");

    }

谢谢:)

解决方法

要为列表框中的项目着色,您需要重写“ ListBox.DrawItem”事件。

只需将其复制到您的班级中:

private readonly List<Color> colors = new List<Color>();
private readonly List<string> texts = new List<string>();
private void ListBox_Draw(object sender,DrawItemEventArgs e)
{
    if (e.Index == -1) return;
    e.DrawBackground();
    Graphics g = e.Graphics;
    //if the item is the maximum temperature item,draw with your color
    g.FillRectangle(new SolidBrush(colors[e.Index]),e.Bounds);
    g.DrawString(texts[e.Index],e.Font,new SolidBrush(e.ForeColor),e.Bounds);
    e.DrawFocusRectangle();
}

private void AddItem(string text,Color color)
{
    colors.Add(color);
    texts.Add(text);
    listBox1.Items.Add("");
}

并将其复制到Form_Load函数中:

listBox1.DrawMode = DrawMode.OwnerDrawVariable;//draw items manually
listBox1.DrawItem += ListBox_Draw;//subscribe to draw event

如何使用它:

您无需使用listBox1.Items.Add(),而只需使用AddItem(您的文本,the_color_of_this_item)

示例:

AddItem("Item 1",Color.Red);
AddItem("This is item 2",Color.White);
AddItem("Hello from item 3",Color.Blue);
AddItem("I am item 4",Color.Purple);
AddItem("is it item 5?",Color.Yellow);
AddItem("Im the last item :(",Color.Green);

这是示例示例:

希望这会有所帮助!