James Finch

James Finch

  • NA
  • 3
  • 24.4k

RichTextBox bold, italic and underline functions

Aug 4 2009 8:45 PM
Hi peeps.

Here's my problem:

I'm creating a text processing app using a RichTextBox for the editing area. I can easily apply the bold, italic or underline style (or a combination) to sections of text.

Woot!

The problem is that it does not work when I try to, say, bold a section of text that comprises two different fonts. I can make it so that it will work, but it changes all selected text to the same font type. This is not what a user would want, and it is not what I want. I can also make it work on sections of text as long as the same font is being used for each character. This is very limited and strange behavior.

I want Word-like behaviour. I.e. I want to apply bold, italic, whatever, to a selection of text and retain its font, size, color, etc.

I know for a fact there is a way to do this, as I had it working a year or two ago. Unfortunately I lost the code because I forgot to back it up before a format / reinstall. /me cries. lol. It worked perfectly using a solution I found on the Net. It was only a few lines and it used arithmetic operators, is all I remember. There were a lot of replies banging on about not using arithmetic operators, etc, etc. But it worked perfectly in every case. I just can't for the life of me remember what I entered into Google to come up with it.

Anyway, like I said, I know there is a way to do this and it doesn't involve looping through characters. If anyone can tell me how to do this, I would be most grateful. I don't mind using Windows APIs, whatever it takes. This is a common requirement, and I'm surprised it's so damned difficult to figure out!

I would prefer any code examples to be in C#, as I am not as adept with VB .NET. Thanks in advance for your time.

Answers (7)