c# - How do I maintain RichText formatting (bold/italic/etc) when changing any one element? -


I have a rich text box that can contain a string in which bold, italic, or even different - Different fonts and sizes are elements. If I select the entire string, which includes all the differences, how can I "bold", without converting the string into the normal font with the "bold" attribute?

For example: I want " this some text to some < Code> text "to turn on"

Note that "something" has been italic and "text" is making a different font.

I currently have considerable simplicity:

  private zero tsBold_Click (object sender, EventArgs e) {if (rtb.SelectionFont == blank) return; Font f; If (tsBold.Checked) f = new font (rtb.SelectionFont, FontStyle.Bold); Else f = new font (rtb.SelectionFont, FontStyle.Regular); RTB. Selectionfont = f; Rtb.Focus (); }   

Of course, this is going to implement the exact same font for the entire selection. Is there any way to add "bold" to the current font (BO)?

Answer The only "official" answer given below was the tip of the iceberg, it was pushing I thank you for the tip tip in the right direction.

This is my official correction:

I added it to my RichTextBox object:

  /// & lt; Summary & gt; /// Change the rich textbox style for the current selection /// & lt; / Summary & gt; Public Zero ChangeFontStyle (FontStyle style, bool add) {// This method should handle cases when multiple fonts / styles are selected // parameter: - // style - such as FontStyle.Bold // add - if If true, then add and remove // ​​If there is no style throw error: bold, italic, strike out or underline (style! = Font style.bold & style! = Font style italic & style! Font =) Styles and Style! = FontStyle.Underline) Throwing a new system. Valid Program Exception ("Invalid Style Parameter for ChangeFontStyle"); Int rtb1start = this.SelectionStart; Int len ​​= this.SelectionLength; Int rtbTempStart = 0; // if len & lt; = 1 and there is a selection font then just handle and return (lane and lt; = 1 & .selectionFont! = Tap) {// Add or remove the style (add this) SelectionFont = new font (this SelectionFont, SelectionFont.Style | style); and this. Selection Font = New Font (Select this font, select it. Style and Style); Return; } (EnhancedRichTextBox using rtbTemp = new EnhancedRichTextBox ()) {// Step one through a selected text at a time rtbTemp.Rtf = this.SelectedRtf; For (int i = 0; i   

Then I changed the click methods to use the following methods:

  Private zero. ChangeFontStyle (FontStyle Bold, TS Bold Check); EnhancedRichTextBox1.Focus (); }    

RTB not supports this well You can not even see the range of characters within that selection, which has the same font style. Start by checking the first selection of the property property, if the selection is a mixture of styles, then it will be zero. If this is the case, then you have to set the selection start and selection gender property and once it will have to repeat the selection one character. As long as this change does not happen, read the selection font. Apply the changed font to the category you searched for.

Check to keep it free from flicker and flicker.

Note that executing an editor with RTB is a favorite topic codeproject.com. The credit code, if not the whole project, is a good way to reduce the pain.

Comments