Monday, 15 July 2013

c# - Change RichTextBox font family only not FontSize -


How can I change the font family only in RichTextBox (RTB) to keep the font size unchanged? I have an RTB that can read in different font and font sizes, I want to change all the fonts to create a new font family, but the user has to maintain whatever size they have. How can I get this? I know about a SelectionFont property, but every overload of the font constructor also requires font size as well.

  RichTextBox RTB = new RichTextBox (); // Some codes that add different text of different fonts and font sizes rtb.SelectAll (); Rtb.SelectionFont = New Font ("Aerial", ??); Besides, I have come to the solution of Lars Tech, but I think it is very complex and there is too much work to achieve this facility. 

If you want to avoid the switching solution, you will have to loop through characters individually. To avoid flicker, you can use Offset Rich Textbox Control to apply changes:

 using  (Rich Textbox TMPRB = New Rich Textbox ()) {tmpRB. SelectAll (); TmpRB.SelectedRtf = rtb.SelectedRtf; For (int i = 0; i & lt; tmpRB.TextLength; ++ i) {tmpRB.Select (i, 1); TmpRB.SelectionFont = New Font ("Aerial", tmpRB.SelectionFont.Size); } TmpRB.SelectAll (); RTB. Selected RTF = TMPRB Chosen RTF; }  

No comments:

Post a Comment