In VB 2005, I'm using a RichTextBox control. I have a button for making all selected text upper-case. The only way I can seem to do this is by setting the RichTextBox's .SelectedText property = .SelectedText.ToUpper.

While this accomplishes the goal of capitalizing the text, it defaults the formatting and color of the text to the RichTextBox's .Font and .ForeColor properties. It does this whether there are multiple formats/colors among the selected text or not.

I need for the format(s) and color(s) of the selected text to be retained, especially if I ever want to make the Replace portion of a Find/Replace method useful at all in the future (let alone this upper-casing method). How can I do this?

I would suggest creating some public class level variables that hold the properties of the RichTextBox's and then create a private sub that "resets" those properties that are changing after your do your text case change.

I can't think of how to store the unknown, variable amount of combinations of font faces, sizes, styles, and colors contained within whatever happens to be selected.

What I mean is, if all of the selected text were the same format, your suggestion would be perfect (and I wouldn't have asked anything here). I'd be able to just store the selected text's font face, size, etc. before I change the SelectedText property, then change the selected font formatting back to what I've stored.

But there could be multiple formats in any configuration among the selected text. In this case, trying to assign the SelectionFont to some variable won't work, since it = Nothing.

The only way I can think of literally is to create a new RichTextBox on the spot, storing/copying(to the new RTB)/reseting(in the new RTB) all formats of each character of the selected text one at a time, then copying the new .Rtf back over. In practice, this method seems to take a ridiculous number of operations to complete (taking 30 seconds to capitalize <10000 characters, all of the same formatting) – there must be a better way.

This article has been dead for over six months. Start a new discussion instead.