We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Platform : Windows Forms
|
Category : RichTextBox

How can I change the FontStyle of a selection without losing the styles that are present

If you visit the selection a character at the time, you can get the current FontStyle and modify it directly to add or remove a style like Bold or Italic. Doing it a character at a time will avoid losing the other styles that are set. The problem with doing it a whole selection at a time is that the FontStyle of the entire selection is the common styles set among all characters in the selection. So, if one char is bold and one is not, then bold is not set when you retrieve the fontstyle for the entire selection. Doing things a character at the time, avoids this problem and allows things to work OK. You can download a working sample(CS,VB). Here is a suggestion from Nicholas Clark on how to avoid seeing the character by character changes appear as you apply the styles one character at a time in the above code. Create a hidden RTB and store the selected text in it. Then apply the attributes and copy it back into the original richtextbox, so that you don’t see the selection changing.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.