473,396 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

doing syntax highlighting

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
4 2777

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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jay Davis | last post by:
I use xemacs and syntax highlighting for my IDE, but I'm not a big fan of the default syntax highlighting colors. For instance, in 'def fun():' both 'def' and 'fun' are the same color. I kind of...
9
by: news.microsoft.com | last post by:
Dear all, I have to do a source code editor to implement syntax hilighted with special colors , IntelliSense function. Could anybody give me some tips or links? Thanks,
1
by: Nic | last post by:
Hi - I am battling to find the a resource, so maybe some one in here can help The problem is as follows: I am trying to build a mod_perl source code editor for the web - to edit Perl source code...
4
by: frikker | last post by:
Hello, I have an idea for a project which involves an editor that supports syntax highlighting. This would be for any language, particularly php, html, css, etc. I would like to write this...
0
by: bsodano | last post by:
Wondering if anyone knew how to have the ability to have syntax highlighting and Intellisense dropdowns for classic ASP 3.0 pages from within Visual Studio 2005. The catch is, we are not allowed to...
11
by: Christoph Burschka | last post by:
Are there any free PHP libraries/utility functions that can color the syntax of various programming languages (eg. Java and C++)? I am posting code snippets on my site and would like to know if...
0
by: Scott | last post by:
Hi, I think I may have finally found a IDE/text editor that I like, but, it still haves one problem. Geany haves syntax highlighting, but it is not very good for Python. It only seems to have...
4
by: Rob Stevens | last post by:
Does anyone have any samples on how to do syntax highlighting? I want to write a small program that will display sources like c#, c++ etc. But when I load the file to store it in a db, I would...
2
by: PJ6 | last post by:
Years ago I looked for a text editor control that would do automatic keyword highlighting, to no avail. I found sample code to roll my own, but all of that was crap (mostly because of the way the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.