简体   繁体   中英

Change font of text in 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. First text of parag1. Second text of parag1. Or First text of parag4. Second text of parag4. 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. First text of parag3. Second text of parag3.

How can I do it in c# ?(Changes font of all text at once!)

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.

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