By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

doing syntax highlighting

P: n/a
Arrrgh!
I have tried everything (ok, not EVERYTHING) but i cant get solve the
problem of getting syntax highlighting in a rich textbox.
in the code below, im attempting to highlight all of the words "ax".
the matches works fine, but i cant get the highlighting to work correctly.
any help?
thanks,
patrick

Private Sub rtbCode_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles rtbCode.TextChanged

Dim code As String = rtbCode.Text

Dim match As MatchCollection

Dim i As Integer

match = Regex.Matches(code, "ax")

Dim results(match.Count - 1) As String

For i = 0 To results.Length - 1

results(i) = match(i).Index

rtbCode.SelectionStart = match(i).Index

rtbCode.SelectionLength = 3

rtbCode.SelectionColor = Color.Blue

Next

End Sub
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Patrick Porter wrote:
Arrrgh!
I have tried everything (ok, not EVERYTHING) but i cant get solve the problem of getting syntax highlighting in a rich textbox.
in the code below, im attempting to highlight all of the words "ax".
the matches works fine, but i cant get the highlighting to work correctly. any help?


1) [the main point] Since you are going to be altering the Selection
details of your TextBox, you need to make sure you save what they were
before your changes, and restore them after you are done.

2) When I loaded up your code VS.NET immediately gave me wavy blue
underlines, and I saw that results() should be an Integer array; this
suggests you me you are running without Option Strict On. Running in
Strict mode is something you should consider making a permanent
situation. Yes, you have to make explicit all you implicit conversions.
This is generally thought to be a *good* thing :)

3) "ax" is two letters long so SelectionLength should be set to 2 not 3
before the colour is changed.

With those changes in mind here is your new routine:

Private Sub rtbCode_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles rtbCode.TextChanged
Dim SaveSelectionStart As Integer = rtbCode.SelectionStart
Dim SaveSelectionLength As Integer = rtbCode.SelectionLength
Dim SaveSelectionColor As Color = rtbCode.SelectionColor

Dim code As String = rtbCode.Text

Dim match As MatchCollection

Dim i As Integer

match = Regex.Matches(code, "ax")

Dim results(match.Count - 1) As Integer

For i = 0 To results.Length - 1
results(i) = match(i).Index
rtbCode.SelectionStart = match(i).Index
rtbCode.SelectionLength = 2
rtbCode.SelectionColor = Color.Blue
Next

rtbCode.SelectionStart = SaveSelectionStart
rtbCode.SelectionLength = SaveSelectionLength
rtbCode.SelectionColor = SaveSelectionColor
End Sub

It flickers a bit when run from the IDE, less so when run from the
executable. To avoid the flicker I suppose you would move to directly
manipulating the Rtf but this would be more work.

--
Larry Lard
Replies to group please

Nov 21 '05 #2

P: n/a
Thank you SO much...i see exactly what you mean.
To explain my actions:
*I set the selected text to 3 because I was messing around trying to get the
cursor past the "ax".
* Busted !!! I thought i had the ide set for option strict..

again, thanks for the help

patrick
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

Patrick Porter wrote:
Arrrgh!
I have tried everything (ok, not EVERYTHING) but i cant get solve the

problem of getting syntax highlighting in a rich textbox.
in the code below, im attempting to highlight all of the words "ax".
the matches works fine, but i cant get the highlighting to work

correctly.
any help?


1) [the main point] Since you are going to be altering the Selection
details of your TextBox, you need to make sure you save what they were
before your changes, and restore them after you are done.

2) When I loaded up your code VS.NET immediately gave me wavy blue
underlines, and I saw that results() should be an Integer array; this
suggests you me you are running without Option Strict On. Running in
Strict mode is something you should consider making a permanent
situation. Yes, you have to make explicit all you implicit conversions.
This is generally thought to be a *good* thing :)

3) "ax" is two letters long so SelectionLength should be set to 2 not 3
before the colour is changed.

With those changes in mind here is your new routine:

Private Sub rtbCode_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles rtbCode.TextChanged
Dim SaveSelectionStart As Integer = rtbCode.SelectionStart
Dim SaveSelectionLength As Integer = rtbCode.SelectionLength
Dim SaveSelectionColor As Color = rtbCode.SelectionColor

Dim code As String = rtbCode.Text

Dim match As MatchCollection

Dim i As Integer

match = Regex.Matches(code, "ax")

Dim results(match.Count - 1) As Integer

For i = 0 To results.Length - 1
results(i) = match(i).Index
rtbCode.SelectionStart = match(i).Index
rtbCode.SelectionLength = 2
rtbCode.SelectionColor = Color.Blue
Next

rtbCode.SelectionStart = SaveSelectionStart
rtbCode.SelectionLength = SaveSelectionLength
rtbCode.SelectionColor = SaveSelectionColor
End Sub

It flickers a bit when run from the IDE, less so when run from the
executable. To avoid the flicker I suppose you would move to directly
manipulating the Rtf but this would be more work.

--
Larry Lard
Replies to group please

Nov 21 '05 #3

P: n/a
ok, im back.
my approach of using selected text and textchanged is very slow, and scrolls
the rtb everytime the event fires.
is there a different way to approach this?
im not asking for someone to write my code, just a push in the right
direction.
thanks,
patrick
"Patrick Porter" <pa*****@ironknee.net> wrote in message
news:im*****************@twister.rdc-kc.rr.com...
Thank you SO much...i see exactly what you mean.
To explain my actions:
*I set the selected text to 3 because I was messing around trying to get
the cursor past the "ax".
* Busted !!! I thought i had the ide set for option strict..

again, thanks for the help

patrick
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

Patrick Porter wrote:
Arrrgh!
I have tried everything (ok, not EVERYTHING) but i cant get solve the

problem of getting syntax highlighting in a rich textbox.
in the code below, im attempting to highlight all of the words "ax".
the matches works fine, but i cant get the highlighting to work

correctly.
any help?


1) [the main point] Since you are going to be altering the Selection
details of your TextBox, you need to make sure you save what they were
before your changes, and restore them after you are done.

2) When I loaded up your code VS.NET immediately gave me wavy blue
underlines, and I saw that results() should be an Integer array; this
suggests you me you are running without Option Strict On. Running in
Strict mode is something you should consider making a permanent
situation. Yes, you have to make explicit all you implicit conversions.
This is generally thought to be a *good* thing :)

3) "ax" is two letters long so SelectionLength should be set to 2 not 3
before the colour is changed.

With those changes in mind here is your new routine:

Private Sub rtbCode_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles rtbCode.TextChanged
Dim SaveSelectionStart As Integer = rtbCode.SelectionStart
Dim SaveSelectionLength As Integer = rtbCode.SelectionLength
Dim SaveSelectionColor As Color = rtbCode.SelectionColor

Dim code As String = rtbCode.Text

Dim match As MatchCollection

Dim i As Integer

match = Regex.Matches(code, "ax")

Dim results(match.Count - 1) As Integer

For i = 0 To results.Length - 1
results(i) = match(i).Index
rtbCode.SelectionStart = match(i).Index
rtbCode.SelectionLength = 2
rtbCode.SelectionColor = Color.Blue
Next

rtbCode.SelectionStart = SaveSelectionStart
rtbCode.SelectionLength = SaveSelectionLength
rtbCode.SelectionColor = SaveSelectionColor
End Sub

It flickers a bit when run from the IDE, less so when run from the
executable. To avoid the flicker I suppose you would move to directly
manipulating the Rtf but this would be more work.

--
Larry Lard
Replies to group please


Nov 21 '05 #4

P: n/a

The RichTextBox offers a property called Rtf which gets/sets the actual
rich text as a String. If you look at this you will see stuff like
this:

this is normal text
\b this is bold
\i this is italic

I'm not sure what the exact syntax is, but basically this is how rich
text works - by inserting formatting instructions into the regular text
(like a primitive form of HTML if you like). You could try obtaining
the string from .Rtf, parsing it for 'ax', inserting the appropriate
formatting codes, then writing it back to .Rtf. This should avoid the
problems you get when you change the Selection.

To find out what the right control codes are, use WordPad to create a
..rtf file and examine that file using Notepad.
Patrick Porter wrote:
ok, im back.
my approach of using selected text and textchanged is very slow, and scrolls the rtb everytime the event fires.
is there a different way to approach this?
im not asking for someone to write my code, just a push in the right
direction.
thanks,
patrick
"Patrick Porter" <pa*****@ironknee.net> wrote in message
news:im*****************@twister.rdc-kc.rr.com...
Thank you SO much...i see exactly what you mean.
To explain my actions:
*I set the selected text to 3 because I was messing around trying to get the cursor past the "ax".
* Busted !!! I thought i had the ide set for option strict..

again, thanks for the help

patrick
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

Patrick Porter wrote:
Arrrgh!
I have tried everything (ok, not EVERYTHING) but i cant get solve the
problem of getting syntax highlighting in a rich textbox.
in the code below, im attempting to highlight all of the words "ax". the matches works fine, but i cant get the highlighting to work
correctly.
any help?

1) [the main point] Since you are going to be altering the Selection details of your TextBox, you need to make sure you save what they were before your changes, and restore them after you are done.

2) When I loaded up your code VS.NET immediately gave me wavy blue
underlines, and I saw that results() should be an Integer array; this suggests you me you are running without Option Strict On. Running in Strict mode is something you should consider making a permanent
situation. Yes, you have to make explicit all you implicit conversions. This is generally thought to be a *good* thing :)

3) "ax" is two letters long so SelectionLength should be set to 2 not 3 before the colour is changed.

With those changes in mind here is your new routine:

Private Sub rtbCode_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles rtbCode.TextChanged
Dim SaveSelectionStart As Integer = rtbCode.SelectionStart
Dim SaveSelectionLength As Integer = rtbCode.SelectionLength Dim SaveSelectionColor As Color = rtbCode.SelectionColor

Dim code As String = rtbCode.Text

Dim match As MatchCollection

Dim i As Integer

match = Regex.Matches(code, "ax")

Dim results(match.Count - 1) As Integer

For i = 0 To results.Length - 1
results(i) = match(i).Index
rtbCode.SelectionStart = match(i).Index
rtbCode.SelectionLength = 2
rtbCode.SelectionColor = Color.Blue
Next

rtbCode.SelectionStart = SaveSelectionStart
rtbCode.SelectionLength = SaveSelectionLength
rtbCode.SelectionColor = SaveSelectionColor
End Sub

It flickers a bit when run from the IDE, less so when run from the
executable. To avoid the flicker I suppose you would move to directly manipulating the Rtf but this would be more work.

--
Larry Lard
Replies to group please



Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.