Hi,
I just wondering if someone let me know how I can have multi color text in RichTextBox?
Lets say I have two combBoxes as below:

private ComboBox color = new ComboBox();
    color.Items.Add("Red");
    color.Items.Add("Green");
    color.Items.Add("Blue");
//and 
private ComboBox shape = new ComboBox();

    shape.Items.Add("Rect");
    shape.Items.Add("Circle");
    shape.Items.Add("Triangle");

I also have a RichTextBox which has it's own Foreground property set to Black color.
Now I want to add Items from Combobox(color) in RED and Items from Combobox(shape) in GREEN into the RichTextBox while items has been selected in comboboxes.

Could you please give me an idea how I can do that?
Thanks
Behrouz

Recommended Answers

All 2 Replies

A sample on how to color text.

private void button1_Click(object sender, EventArgs e) {
             string text1 = comboBox1.SelectedItem.ToString();
             string text2 = comboBox2.SelectedItem.ToString();

             richTextBox1.Text = String.Empty;

             richTextBox1.AppendText(text1);
             richTextBox1.Select(0, text1.Length);
             richTextBox1.SelectionColor = Color.Red;
             
             richTextBox1.AppendText(" ");

             richTextBox1.AppendText(text2);
             richTextBox1.Select(text1.Length + 1, text2.Length);
             richTextBox1.SelectionColor = Color.Green;
        }
commented: Good Hint +1

Thanks Momerath,
I modified your suggestion a little bit and it works great now.

void ListBox1SelectedIndexChanged(object sender, EventArgs e)
		{
	         string txtR = comboBox1.SelectedItem.ToString();
                 richTextBox1.SelectionColor = Color.Red;
                 richTextBox1.AppendText(txtR + "  ");
		}
		
		void ListBox2SelectedIndexChanged(object sender, EventArgs e)
		{
		 string txtG = comboBox2.SelectedItem.ToString();
                 richTextBox1.SelectionColor = Color.Green;
                 richTextBox1.AppendText(txtG + "  ");
    		}

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.