简体   繁体   中英

Change current color in a richTextBox?

I would like to make a program where you can type in a richTextBox and then change the color.

So I tried this.

private void redFontColor_Click_1(object sender, EventArgs e)
{
   richTxtBox.FrontColor = Color.Red;
}

But when I click the redFontColor button all the text in the richTextbox changes to red. So I tried to change the the color of each character individually.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Color currentColor = Color.Black;

    private void redFontColor_Click_1(object sender, EventArgs e)
    {
        currentColor = Color.Red;
    }

    private void richTxtBox_TextChanged(object sender, EventArgs e)
    {
        if (textBox.SelectionStart != 0)
        {
            richTextBox.Select(textBox.SelectionStart - 1, 1);
            richTextBox.SelectionColor = currentColor;
        }
    }
}

But when I now type something the color will change but when you enter a character it will be selected like this:

在此处输入图片说明

How could I make this work so the char isn't selected each time I type something? Or do I have to search for another method to do this?

As for your code: You are selecting a length of 1, ie the last character and you now see it highlighted. This is all not necessary. If you want to set colors or other formatting to text you enter simply do this:

  • Set the SelectionStart to the point he text gets entered, for example the end of the Text
  • Set the SelectionLength to 0
  • Set the formatting you want.

It will carry over to all text that you enter; make sure no line break (which in a RTB really means a new paragraph) slips in, as it will break the automatic carrying over. This all works pretty much like it does in Word..

Btw: Do not call a RichTextBox a Textbox ; yes, they are related but still..

Also: We need to understand the difference between highlighting and selecting !

Selections are meant to format text portions.

Highlighting happens along the way and looks like it is set to look in the system settings.

Here is an example with all you need to get started:

private void panel1_MouseClick(object sender, MouseEventArgs e)
{
    if ( richTextBox1.SelectionLength == 0)
         richTextBox1.SelectionStart = richTextBox1.Text.Length;

    Color c = ((Bitmap)panel1.BackgroundImage).GetPixel(e.X, e.Y);
    if (e.Button.HasFlag(MouseButtons.Left))
    { 
        richTextBox1.SelectionColor = c;
    }
    else
    { 
        richTextBox1.SelectionBackColor = c;
    }
}

Pick a nice color palette and display in in a Panel.BackgroundImage; the code the MouseClick event. Done.

You can keep typing and when you left-click a color the text color will start to appear in the color. When you right-click it the backcolor will change. When there is a selection already it will change its color.

For a full formatting rtb you will want to add other things like font style size etc..

Note: No need to code the TextChanged event for this.

Basically richTxtBox.FrontColor = Color.Red; will change the color of all text to Red , but you requirement is to change only the color of selected text, for that you need to use, richTextBox1.SelectionColor = Color.Red; which will change the color of only selected text.

You can decide which portion of the text need to be selected by using SelectionStart and SelectionLength . it is purely working on the basics of the index.

The mentioned problem of selecting the typed text in richTextBox1_TextChanged can be avoid by using this snippet

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    if (richTextBox1.SelectionStart != 0)
    {
        int length = richTextBox1.Text.Length;
        richTextBox1.Select(richTextBox1.SelectionStart - 1, 1);
        richTextBox1.SelectionColor = Color.Red ;
        richTextBox1.SelectionStart = length;
    }
}

Apart from other answers here i have a magic box, which will changes the colors based on text.

在此处输入图片说明

And this can be achieved by using the following code:

private void button1_Click(object sender, EventArgs e)
{
    int selectionStart = 0;
    foreach (var item in richTextBox1.Text.Split(' '))
    {
        Color rgb = Color.FromName(item);
        richTextBox1.SelectionStart = selectionStart;
        richTextBox1.SelectionLength = item.Length;
        richTextBox1.SelectionColor = rgb;
        selectionStart += item.Length + 1;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM