Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font of text in richtextbox

Tags:

c#

richtextbox

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!)

like image 735
Ali Ahmadi Avatar asked Apr 02 '12 20:04

Ali Ahmadi


People also ask

How to change font RichTextBox?

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.

How do I make text bold in RichTextBox C#?

SelectionFont = new Font(textBox. Font, FontStyle. Bold);

How do I change the font size in a TextBox in C#?

// 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);

How do I add text to RichTextBox?

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.


1 Answers

You would need to select parts of text and use SelectionColorand SelectionFontproperties. 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.

like image 108
phadaphunk Avatar answered Oct 23 '22 17:10

phadaphunk