Give color to text parts of a textBox

0

I have this code:

string Clei=string.Empty;

Clei = "Se confecciona boleta # "+  txtNumeroBoleta.Text + ", del " + a + " al " + b + " (" + dias + " días), firmada por el usuario: " + usuario + " código. " + codigoUsuario + ".";

txtNota.Text=Clei;

The subject is the following, I need to give a different color to the data that comes from the variables and from the other txt that will be shown in the txtNote.

I expect a result like the following:

  

Ballot # 121212 , from 02/07/2018 to 12/07/2018 ( 10 days), signed by the user: Jan Carlos Vargas code. 601230123 .

NOTE : Bold in a different color

    
asked by Magdiel Hernandez 06.10.2018 в 07:58
source

1 answer

0

Using% co_of impossible%, but it's a simple thing to do using RichTextBox with the following extended method

public static class RichTextBoxExtensions
{
    public static void AppendText(this RichTextBox box, string text, Color color)
    {
        box.SelectionStart = box.TextLength;
        box.SelectionLength = 0;    
        box.SelectionColor = color;
        box.AppendText(text);
        box.SelectionColor = box.ForeColor;
    }
}

Then, you simply use this extension in the following way

//Color rojo
richTextBox1.AppendText("Texto en color rojo!", Color.Red);
//Color negro
richTextBox1.AppendText("Texto en color negro!");
//Color verde
richTextBox1.AppendText("Texto en color rojo!", Color.Green);

The extension method, take it from the answer by Nathan Baulch at StackOverflow.com

    
answered by 08.10.2018 / 14:30
source