How do I change the font color on a rich textbox while I'm typing?
Printable View
How do I change the font color on a rich textbox while I'm typing?
I'm not sure if this what you were asking but anyhow:
VB Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged RichTextBox1.ForeColor = Drawing.Color.Blue End Sub
Sorry. I mean on selected keywords. I'll re-phrase it.
How do I change the font color on selected keywords on a rich textbox while I'm typing? Keywords such as "SELECT" or "QUERY."
VB Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged RichTextBox1.Find("STRING", RichTextBox1.SelectionStart - 6, RichTextBoxFinds.None) RichTextBox1.SelectionColor = Drawing.Color.Red RichTextBox1.Select(RichTextBox1.Text.Length, 0) RichTextBox1.SelectionColor = Drawing.Color.Black End Sub
Thanks but the code you gave me yields a problem. When I try to change the cursor position and enter a single character, it will immediately shift my cursor to the end of the text.
are you trying to do a IDE like textbox? that will color words? that might give you a lot of trouble, the best way to do it maybe would be superclass the base textbox control and not the richtextbox control as it is slow for all this kind of operations
Yes! But I don't know how to do what you just said.Quote:
Originally posted by PT Exorcist
are you trying to do a IDE like textbox? that will color words? that might give you a lot of trouble, the best way to do it maybe would be superclass the base textbox control and not the richtextbox control as it is slow for all this kind of operations
I hope this is the code that will solve your problem.
GodLovesYou Your code was messed up.
VB Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged If InStr(richtextbox1.text, "STRING", CompareMethod.Binary) Then RichTextBox1.Find("STRING", RichTextBox1.SelectionStart - 6, RichTextBoxFinds.None) RichTextBox1.SelectionColor = Drawing.Color.Red End If RichTextBox1.Select(RichTextBox1.Text.Length, 0) RichTextBox1.SelectionColor = Drawing.Color.Black End Sub
Remember, when comparing by binary, all selections will be case-sensative. If you want me to create the complete code for you, just send me a private message.
i didnt try it but it doesnt look to me as it would ever work as in the end you are selecting the end of the richtextbox...if he is writing in the middle then it wont workQuote:
Originally posted by Danny J
I hope this is the code that will solve your problem.
GodLovesYou Your code was messed up.
VB Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged If InStr(richtextbox1.text, "STRING", CompareMethod.Binary) Then RichTextBox1.Find("STRING", RichTextBox1.SelectionStart - 6, RichTextBoxFinds.None) RichTextBox1.SelectionColor = Drawing.Color.Red End If RichTextBox1.Select(RichTextBox1.Text.Length, 0) RichTextBox1.SelectionColor = Drawing.Color.Black End Sub
Remember, when comparing by binary, all selections will be case-sensative. If you want me to create the complete code for you, just send me a private message.
change it to this:
i dont have vs.net here so i cant test it but it would be something like thisVB Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged dim pos as integer = richtextbox1.position 'or whatever property it has to get the position If InStr(richtextbox1.text, "STRING", CompareMethod.Binary) Then RichTextBox1.Find("STRING", RichTextBox1.SelectionStart - 6, RichTextBoxFinds.None) RichTextBox1.SelectionColor = Drawing.Color.Red End If RichTextBox1.Select(pos, 0) RichTextBox1.SelectionColor = Drawing.Color.Black End Sub