473,395 Members | 1,987 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,395 software developers and data experts.

Syntax Highlighting RichTextBox...

I've been developing in vb.net for a few months now, well since i left school and one of my projects for my current company is to create a sql tool which is used on a daily basis to update, delete and select from the database.

One of the functions i want this program to do is to highlight all sql words to make the program look alot 'cleaner'.

I have achieved this to a point, I am currently at the stage where my code works, but not well or fast.

Here is my current code for the textchanged event of the richtextbox which I would like to speed up alot:

Private Sub RTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtSql.TextChanged

Dim colours(2) As String
Dim BlueText(25) As String
Dim RedText(13) As String
Dim greentext(5) As String
Dim Find As Integer
Dim Beginhere As Integer = 0
Dim Length As Integer

TxtSql.SuspendLayout()
TxtSql.SelectAll()
TxtSql.SelectionColor = Color.Black
TxtSql.SelectionFont = New Font("Tahoma", 8, FontStyle.Regular)

'Fills the different syntax colours.
colours(0) = "Blue"
colours(1) = "Red"
colours(2) = "Green"

'Fills all the syntax which will be purple.
BlueText(0) = "select"
BlueText(1) = "delete"
BlueText(2) = "update"
BlueText(3) = "from"
BlueText(4) = "totext"
BlueText(5) = "tochar"
BlueText(6) = "where"
BlueText(7) = "and"
BlueText(8) = "like"
BlueText(9) = "date"
BlueText(10) = "time"
BlueText(10) = "between"
BlueText(11) = "desc"
BlueText(12) = "asc"
BlueText(13) = "order by"
BlueText(14) = "group by"
BlueText(15) = "distinct"
BlueText(16) = "count"
BlueText(17) = "sum"
BlueText(18) = "distinct"
BlueText(19) = "in"
BlueText(20) = "min"
BlueText(21) = "max"
BlueText(22) = "not"
BlueText(23) = "or"
BlueText(24) = "set"
BlueText(24) = "only"
BlueText(25) = "as"

'Fills all the syntax which will be red.
RedText(0) = "<"
RedText(1) = ">"
RedText(2) = "="
RedText(3) = "("
RedText(4) = ")"
RedText(5) = "["
RedText(6) = "]"
RedText(7) = "*"
RedText(8) = "%"
RedText(9) = "&"
RedText(10) = ";"
RedText(11) = ":"
RedText(12) = "|"
RedText(13) = ","

'Fills all the syntax which will be green.
greentext(0) = "'"
greentext(1) = Chr(34)
greentext(2) = "union"
greentext(3) = "union all"
greentext(4) = "null"
greentext(5) = "is null"

'J is an integer which will loop between the three different colours for the checking procedure.
Dim j As Integer = 0
For j = 0 To colours.Length - 1

' I is an integer which will loop through the strings in each colour text.
Dim i As Integer = 0

If j = 0 Then

Length = BlueText(i).Length
For i = 0 To BlueText.Length - 1
Beginhere = 0

Do
Find = TxtSql.Find(BlueText(i), Beginhere, RichTextBoxFinds.WholeWord)

If Find > -1 Then
TxtSql.SelectionColor = Color.Purple
TxtSql.SelectionFont = New Font("Tahoma", 8, FontStyle.Bold)
End If

Beginhere = Beginhere + Length
If Beginhere > Len(TxtSql.Text) Then Exit Do

Loop While Find > -1

Next
End If

If j = 1 Then
Length = RedText(i).Length
For i = 0 To RedText.Length - 1
Beginhere = 0

Do
Find = TxtSql.Find(RedText(i), Beginhere, RichTextBoxFinds.None)

If Find > -1 Then
TxtSql.SelectionColor = Color.Crimson
End If


Beginhere = Beginhere + Length
If Beginhere > Len(TxtSql.Text) Then Exit Do

Loop While Find > -1

Next
End If


If j = 2 Then
Length = greentext(i).Length
For i = 0 To greentext.Length - 1
Beginhere = 0

Do
Find = TxtSql.Find(greentext(i), Beginhere, RichTextBoxFinds.None)

If Find > -1 Then
TxtSql.SelectionColor = Color.Green
End If


Beginhere = Beginhere + Length
If Beginhere > Len(TxtSql.Text) Then Exit Do

Loop While Find > -1

Next
End If

Next

'Sets the following text back to normal font and colour.
TxtSql.Select(Len(TxtSql.Text) + 1, 0)
TxtSql.SelectionColor = Color.Black
TxtSql.SelectionFont = New Font("Tahoma", 8, FontStyle.Regular)
TxtSql.ResumeLayout()


End Sub


My aim is to speed this up alot because it is very slow with large amounts of text.

Thanks, Danny.
Jun 11 '07 #1
0 2514

Sign in to post your reply or Sign up for a free account.

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...
4
by: Bob hotmail.com> | last post by:
Everyone I have been spending weeks looking on the web for a good tutorial on how to use regular expressions and other methods to satisfy my craving for learning how to do FAST c-style syntax...
2
by: Johnny | last post by:
Sorry I posted this post earlier but got the name and subject the wrong way round (new to this!) so it perhaps didn't look right Sorry for that. But please please answer if you know anything about...
4
by: Patrick Porter | last post by:
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...
3
by: Patrick Porter | last post by:
I am looking at followin the suggestion made by Larry on my last post. (his reply is on the bottom) it seems that you can't directly mess with the rich text header text....specifically the addtion...
2
by: rockstar_ | last post by:
Hello all- I'm developing a Content Management software for my own site, and possibly package and deploy to other sites (for friends, family, etc.) The content management software is combined...
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...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.