Work consists that I put on different pieces of the text different styles and type size. And then select all the changed pieces and put them one font. The result should be: the text is selected in the ComboBox font, and their styles and sizes do not change without authorization. At the moment everything happens the other way around. I put pieces of text in different styles and sizes, select everything, put all one font and all previous formatting flies.

Here is what is available:

 private void FontBox_SelectedIndexChanged(object sender, EventArgs e) { if (RichTextBox.SelectionFont != null) { RichTextBox.SelectionFont = new Font(FontBox.Text, RichTextBox.SelectionFont.Size, RichTextBox.SelectionFont.Style); } RichTextBox.Select(); } private void SizeBox_SelectedIndexChanged(object sender, EventArgs e) { float size = 12; if (SizeBox.Text != string.Empty) { size = Convert.ToSingle(SizeBox.Text); } RichTextBox.SelectionFont = new Font(RichTextBox.SelectionFont.FontFamily, size, RichTextBox.SelectionFont.Style); } 

What is the error I know. The program assigns all selected one size and one style. The question is different. How to make the program keep its size and style when assigning new Font() ? Is it really necessary to do everything character by character through a cycle?

PS Also, by the way, it should work when I select the text size, also through the ComboBox .

    1 answer 1

    1. The concept of a font in .NET includes almost all the font parameters, with the exception of text and background colors.

    2. System.Drawing.Font is immutable, it can be seen from the fact that all its properties are read-only. Therefore, you cannot change the created font, for example, the size is not creating a new Font object for it.

    3. Certainly not worth it, because such a solution is likely to generate terrible text rtf markup. To solve your problem, you will most likely have to deal with the RTF format and do everything at a low level, i.e. rtf markup with "hands"; RichTextBox , in spite of all its advantages, is just a basic element that supports RTF display and supports modest text quite modestly.