[2005] How To Change the Color of a Typed Word in a RTB
I am making a coding assistant program, and I want to make it so that after a user presses the space bar, if the last word typed is one that the program recognizes, it will turn it to the color specified.
Basically, I want to do syntax highlighting. If there is any easier way than this please tell me, or else post some code.
Re: [2005] How To Change the Color of a Typed Word in a RTB
I don't know how, but I did a search and found a sample for something similar. Perhaps this can guide you.
http://pietschsoft.com/Blog/Post.aspx?PostID=491
Re: [2005] How To Change the Color of a Typed Word in a RTB
Great, but, How do I use it? :)
Re: [2005] How To Change the Color of a Typed Word in a RTB
That person there is creating an inherited control. The control has been inherited from the existing RTB control, he's just adding his own routines to it and overriding a few existing routines.
You could start by... copy-pasting that code into a windows control project class library, compiling it, and then adding that control to another project ( a test project). Then start hacking away at the code.
Re: [2005] How To Change the Color of a Typed Word in a RTB
Quote:
Originally Posted by Seraphino
I am making a coding assistant program, and I want to make it so that after a user presses the space bar, if the last word typed is one that the program recognizes, it will turn it to the color specified.
Basically, I want to do syntax highlighting. If there is any easier way than this please tell me, or else post some code.
Hi,
I think you can use the ColorDialog for that, here's an exa^mple with a RichTextBox and a Button.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ColorDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
rtxtEditor.ForeColor = ColorDialog1.Color
End If
End Sub
Hope it helps,
sparrow1
Re: [2005] How To Change the Color of a Typed Word in a RTB
Nope, this isn't what I wanted, but thanks for the tip!
Re: [2005] How To Change the Color of a Typed Word in a RTB
Try
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ColorDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
rtxtEditor.SelectionColor = ColorDialog1.Color
End If
End Sub
Re: [2005] How To Change the Color of a Typed Word in a RTB
Ken B is right, there is no other way to change the color of only some letters in a generic richtextbox unless you select them first and then use "richtextbox.SelectionColor = Color.some color"
(I'd be gladly wrong, since the whole select business is kind of fishy, one unintented click from the user can wreak havoc in your formatting logic)
Re: [2005] How To Change the Color of a Typed Word in a RTB
Quote:
(I'd be gladly wrong, since the whole select business is kind of fishy, one unintented click from the user can wreak havoc in your formatting logic)
The click won’t change the selection property of the Rich Tex Box. For example the “SelectionText” of the RichTextBox would always be the last word in the control unless it is changed manually.
Re: [2005] How To Change the Color of a Typed Word in a RTB
Quote:
Originally Posted by VBDT
The click won’t change the selection property of the Rich Tex Box. For example the “SelectionText” of the RichTextBox would always be the last word in the control unless it is changed manually.
I just checked it, RichTextBox1.Select(0, 8) -> user clicks inside -> RichTextBox1.SelectionColor = Color.Red doesn't change the color of the previous selection to red.
Re: [2005] How To Change the Color of a Typed Word in a RTB
Yes you are right! On the click event “SelectionText’s” index is set to 0. Never mind my previous comment.