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.