My text is in a richtextbox:
<Parag1 Level="One">
First text of parag1. Second text of parag1.
</Parag1>
<Parag2 Level="Two">
First text of parag2. Second text of parag2.
</Parag2>
<Parag3 Level="Footer">
First text of parag3. Second text of parag3.
</Parag3>
<Parag4 Level="Three">
First text of parag4. Second text of parag4.
</Parag4>
I want change color font & text color of text :
1- For tags -> font name = Tahoma , size= 10,color=red
Example : <Parag1 Level="One">
Or </Parag1>
2- For Text between tags that tag's level is not Footer -> font name = Arial , size= 12,color=black
Example : First text of parag1. Second text of parag1.
Or First text of parag4. Second text of parag4.
3- For Text between tags that tag's level is Footer -> font name = Microsoft Sans Serif, size= 8,color=blue
Example : First text of parag3. Second text of parag3.
How can I do it in c# ?(Changes font of all text at once!)
The Windows Forms RichTextBox control has numerous options for formatting the text it displays. You can make the selected characters bold, underlined, or italic, using the SelectionFont property. You can also use this property to change the size and typeface of the selected characters.
SelectionFont = new Font(textBox. Font, FontStyle. Bold);
// Creating textbox TextBox Mytextbox = new TextBox(); Step 2 : After creating TextBox, set the Font property of the TextBox provided by the TextBox class. // Set Font property Mytextbox. Font = new Font("Broadway", 12);
Step 2: Drag the RichTextBox control from the ToolBox and drop it on the windows form. You are allowed to place a RichTextBox control anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the RichTextBox control to add text in the RichTextBox control.
You would need to select parts of text and use SelectionColor
and SelectionFont
properties.
Everything is explained here.
Hope this helps
Now for your other question, if you mean how to change the font and color of the text inserted while the program is running, try this.
private void someTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
this.someTextBox.SelectionColor = Color.Blue;
// Same goes for font and other properties
}
I don't have time to test it so I don't know how it will act with the other colors you previously set.
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