I want show text in textbox in 2 colors, for example 1 line red 2 blue, if I use name.ForeColor = Color.Red;
all text change color, but I want that will change only 1 line color.
Use a RichTextBox for that, here is an extension method by Nathan Baulch
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;
}
}
Read more here
You need to use a RichTextBox.
You can then change the textcolor by selecting text and changing the selection color or font.
richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;
Here is an example with a Fontdialog and Colordialog.
void TextfarbeToolStripMenuItemClick(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
richTextBox1.ForeColor = colorDialog1.Color;
listBox1.ForeColor = colorDialog1.Color;
}
void FontsToolStripMenuItemClick(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
richTextBox1.Font = fontDialog1.Font;
listBox1.Font = fontDialog1.Font;
}
void HintergrundfarbeToolStripMenuItemClick(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
richTextBox1.BackColor = colorDialog1.Color;
listBox1.BackColor = colorDialog1.Color;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With