Click here to Skip to main content
15,851,711 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
Can you please explain me how to change font and colour of selected text in RichTextBox? I was searching for long time and didn't find any solution. Only how to change font of entire TextBox and that's not what I want. If I could save the text to file with a new font it would be ideal :)
Thanks for any kind of help

Edit: I've done some experimenting
C#
FontDialog fd1 = new FontDialog();

 private void edit_Click(object sender, EventArgs e)
 {
     fd1.Font = textbox.SelectionFont;

     if(fd1.ShowDialog() != DialogResult.Cancel)
     {
       textbox.Font = fd1.Font;
     }
 }


but it looks like it saves it with the default font
Posted
Updated 14-Mar-17 8:09am
v10
Comments
Shahin Khorshidnia 21-Mar-12 17:52pm    
Please tag your question. WinForm or WPF?

You need to select some part of document using properties SelectionStart and SelectionLength. Then, use the properties like SelectionColor, SelectionBackColor and the like.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.aspx[^].

—SA
 
Share this answer
 
Comments
LosEagle 22-Mar-12 12:05pm    
Ok, thanks to your and Dalek's answer I figured out how to change font of selected text. But when I want to save the file I don't know much how to do because it seems like ReadAll (when I want to see saved file) and WriteAll are just reading and saving the file in default font. Should I create FileStreams?
Sergey Alexandrovich Kryukov 22-Mar-12 15:54pm    
What, RichBox.SaveFile? Here:
http://msdn.microsoft.com/en-us/library/8bf5hy2e.aspx
--SA
LosEagle 22-Mar-12 18:36pm    
Thanks, very useful codes are there :). But I still have the problem with saving font of text. What I'm trying to do is application which can save the text with changed font and when I load it again the font will appear as I changed it in application. I used the advices from great answers here and I can change the selected font in application but when I save it in txt for example and load it again using readalltext and the font in richtextbox is the last font I used in changing selected text :-/ Well, at least I can temporary change the font of selected text without saving
Sergey Alexandrovich Kryukov 22-Mar-12 20:14pm    
Hard to say, it could be some bug or something. If you save the RTF as a full document, you save it all, with all formatting/styles. Could be some logistics, whatever... you need to investigate it on simple samples. Look at the RTF manually, with word pad and plain-text editor, compare...
--SA
 
Share this answer
 
Comments
Shahin Khorshidnia 21-Mar-12 17:56pm    
Good link but OP wants to save the text to a file, and you know that some characters might be in different colors.
LosEagle 21-Mar-12 18:10pm    
Yes sorry that I wrote the important part of my question to end. Your answer was helpful but it saves in default font.
Sergey Alexandrovich Kryukov 21-Mar-12 18:38pm    
Yes, sorry, but you did not read the answer to the end, that's the problem. OK, please first see my answer.
If you want, load the page referenced by Dalek to see that one of the answers actually give you the right idea, you just had to find it.
--SA
Sergey Alexandrovich Kryukov 21-Mar-12 18:39pm    
So, I voted 4 this time, as OP could use some clarification on where to look at, more exactly :-)
That's why I answered, too, to clarify...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900