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

后端 未结 5 780
醉酒成梦
醉酒成梦 2020-12-05 15:52

I have a rich text box that may contain a string that has elements of bold, italics, or even different fonts and sizes. If I select the entire string, including all of the d

5条回答
  •  情深已故
    2020-12-05 16:21

    RTB does not support this well. You cannot even discover the range of characters within the selection that has the same font style. Start by checking the SelectionFont property first, it will be null if the selection contains a mix of styles. If that's the case, you'll have to iterate the selection one character at a time by setting the SelectionStart and SelectionLength properties, read the SelectionFont until it changes. Apply the changed font to the range you discovered.

    Check this answer for a way to keep this reasonably quick and flicker-free.

    Note that implementing an editor with RTB is a favorite topic at codeproject.com. Borrowing code, if not the entire project, is a good way to lessen the pain.

提交回复
热议问题