0

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

Edited by Behseini: n/a

2
Contributors
2
Replies
8
Views
6 Years
Discussion Span
Last Post by Behseini
1

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;
        }

Edited by Momerath: n/a

Votes + Comments
Good Hint
0

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

Edited by Behseini: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.