More problems with a RichTextBox: I can't change the text color!

DevilAkuma

Active member
Joined
Oct 24, 2005
Messages
37
Programming Experience
Beginner
I only want to change the color of a word has been typed by the user...

I'm dissapointed with this damned control. I don't know if it's plenty of bugs or I don't know how it works.

In a previous post, I asked how can I know where I was (the position, of course) Well, I got it with this when the user types a space.

VB.NET:
Dim endWord As Integer = myRich.SelectionStart

With this position, I'm reading (letter by letter) decreasing this endWord until I read another space (or enter, or chars like that). Then, I that situation I have the numberLetters (The number of letters that I decreased from endWord) and the endWord of a word (Just typed by the user) Then I do this.

VB.NET:
myRich.Refresh()
myRich.SelectionStart() = endWord - numberLetters
myRich.SelectionLength() = numberLetters
myRich.SelectionColor() = Color.Red

If I do this... The word (just typed) dissapear! And I don't know why! After that, If I try to write, the color of the letters are red :(
The I try to append some text...

VB.NET:
 myRich.Refresh()
 myRich.SelectionStart() = endWord - numberLetters
 myRich.SelectionLength() = numberLetters
 myRich.SelectionColor() = Color.Red
myRich.AppendText("")

Don't ask mw why, but with this code runs better. The problem now is that the cursor moves until the end of the text. My function that hanles the myRich.VScroll is called (and I don't know why!)

I only want to change the color of a word has been typed by the user...

Any idea?

Thanks in advance!
 
Real-time coloring of current word may not be a good solution, consider all the possibilities where user deletes what was a text delimiter (like space,.- enter=), handling mouse clicks/selection/edits...
Anyway, look at this very short and simple code example, I have a RichTextBox named 'rtb' and use the KeyUp event to check for space delimiter. It colors a 'word' blue after it is written and sets color to continue writing with to black.
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] wordstart [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] wordend [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] rtb_KeyUp([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] rtb.KeyUp
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] e.KeyCode = Keys.Space [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]wordend = rtb.SelectionStart
rtb.Select(wordstart, wordend - wordstart)
rtb.SelectionColor = Color.Blue
rtb.SelectionStart = wordend
rtb.SelectionColor = Color.Black
wordstart = wordend
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Thanks for your help, JohnH.

The reason that I try to change my color's letters after the word is typed is that I only change some words (Like 'If', 'Then'... the typical code words).

And with your code, when I press spacebar in the middle of any text, all the text until the space that I just typed changes its color into blue. In your project runs well?

Thanks a lot!
 
Yes, for the basic example it does.
Use the head and your logic sense, man! Of course if you open some existing file and put the cursor in the middle you won't have set the wordstart variable like in the example. What if you on mousedown event set this indexer variable to current caret position? (rtb.selectionstart) Then writing something will be handled from only that position. Remember, the initial integer variable value is 0.
 
Ok... So... Is it impossible to do that I want? I mean.. If could desactivate the automatic VScroll event... The text color is correctly changed now, the problem is the ugly movement of the scrolls bar.

Is it possible to desactivate it?
 
As a last resort, when you are absolutely 100% sure that there is nothing else you can do to prevent scrolling/flicker with the RichTextBox in your own code logic, you can use the LockWindowUpdate Win32 API method.
LockWindowUpdate will prevent the control/window from being repaint for a short timespan (say split-second).
This method is a system-wide, stricly one-user-at-a-time method.
See link: http://weblogs.asp.net/jdanforth/archive/2004/03/12/88458.aspx

(There is also mentioned in that blog about SendMessage, but I haven't tested that yet.)
 
Sorry, but I'm a beginner and I don't know the way to declare that function in VB.NET :(

I'm trying with this code...

VB.NET:
<DllImport("user32.dll")> Public Function LockWindowUpdate(ByVal hWndLock As IntPtr) As Boolean

But I don't know... the error is something about the spacenames. I can say the exact error in spanish :?

Thanks in advance
 
I can't do it!!!

With your piece of code I can't call these function, but seems that don't do anything :(

I do this:
VB.NET:
LockWindowUpdate(myRichTextBox.Handle.ToInt32)
//My funtions where change the color
LockWindowUpdate(0)
But the funtion that handles the VScroll event is still called!!! And, of course, the scrollbars moves at the bottom :(

Any idea? Sorry, I'm a bit desesperated...

Thanks in advance.
 
I actually have problems understanding how you get this extreme scrolling you describe, when you only select and color the nearest word to current caret position then set the caret back to the end of that same word... that should hardly twinkle the scroll if anything at all...
 
Back
Top