[.net] Changing Font/Color in RichTextBox

Started by
5 comments, last by TheBluMage 19 years, 4 months ago
I'm deriving from the RichTextBox class to create a text box for syntax highlighting. Right now, when the user changes the text, I identify where keywords are, select them, change what the selected font/color is, and then when everything has been highlighted, I change the selection back to what It originally was. The problem is that when I change the selection, the text box scrolls so that location is at the very top. Is there anyway to prevent the text box from scrolling whenever a selection is changed? Also, I there a better method of changing the font/color then going through and selecting everything? It seems kind of hackish - I've already had to catch the WM_PAINT event manually to prevent flicker/jumping and I'm thinking there's got to be a better way. Thanks in advance!
Advertisement
You'll want to take a look at the ScrollToCaret() method and the [Get|Set]ScrollInfo() methods.

Quote:Also, I there a better method of changing the font/color then going through and selecting everything?

Haven't tried it, but what about setting the Font property of the RTB?
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
Quote:Original post by kSquared
...ScrollToCaret() method and the [Get|Set]ScrollInfo() methods.

Thanks for the quick reply. ScrollToCaret() doesn't really help, for example - when the caret is originally in the middle of the screen. (ScrollToCaret puts the caret at the top.) Haven't been any Get/SetScrollInfo methods.[looksaround] Probably in a really obvious spot that I missed... Anyway, I think I'm just going to write my own text box inheriting from UserControl; RichTextBox doesn't quite seem flexible enough to do what I want. Thanks again!
Perhaps this helps you out...

Craig is building his own RichTextEditor...

Cheers
Quote:Original post by TheBluMage
Quote:Original post by kSquared
...ScrollToCaret() method and the [Get|Set]ScrollInfo() methods.

Thanks for the quick reply. ScrollToCaret() doesn't really help, for example - when the caret is originally in the middle of the screen. (ScrollToCaret puts the caret at the top.) Haven't been any Get/SetScrollInfo methods.[looksaround] Probably in a really obvious spot that I missed... Anyway, I think I'm just going to write my own text box inheriting from UserControl; RichTextBox doesn't quite seem flexible enough to do what I want. Thanks again!


Sorry, that was pretty vague. ScrollToCaret(), as you figured out, is a method of the RTB class; [Get|Set]ScrollInfo() is a Windows API function that you can read more about here.

Just so I'm absolutely clear: you want the cursor to not change position when you change a selection, right? I'm a little confused as to why you just don't programmatically set the location of the caret. Did you try that already and it didn't work?

The solution I would use is:

--> 1.) Memorize where the caret is beforehand by storing the value of the SelectionStart property.
--> 2.) Perform the work and changes you want. Make note of the length of the value you changed to and store it. For instance, if you changed "zonkers" to "foobletastic", this value would be 12.
--> 3.) Once you're done making changes, change the SelectionStart value to its old value, and add the length you stored in step 2.
--> 4.) Finally, set the SelectionLength property to zero. This moves the caret to the SelectionStart location. Call ScrollToCaret() to make sure the caret's in view.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
You could have an invisible RichTextBox and do all your Hightlighting operations there. When you're done, just copy the blind RichTextBox's Rtf property to the real RichTextBox. This helps you avoid the flickering you get when changing the selection programmatically..
I tried out the Get/SetScrollInfo methods and they don't seem to like the handle of the rich text box (not sure how to get a handle to the scroll bars). I tried having an invisible box in memory, but when I change the Rtf property of the visible box the cursor gets moved and it gets scrolled up to the top, so same problem. Thanks for all the ideas, but I think I'm just gonna start from scratch and do a customized control just for syntax highlighting, RichTextBox doesn't really seem like it was designed with this in mind. Thanks again!

This topic is closed to new replies.

Advertisement