Auto Style Words in TextArea

Hey all I am making a little SQL query textarea in my app. I am stylizing the text based on keywords found and I am not to a point where I want to make strings (any characters in single quotes, including the single quotes) be red.

Below is the code I am using (I realize it isnt the most optimized way to do this but I dont need it to stylize as Im typing) for turning keywords blue.

I am open to making this whole thing cleaner and run quicker as well so really any help with that and getting the strings to be red would be much appreciated.

Main.QueryEditor1.QueryArea.Visible = False
Var ms As Integer = Main.QueryEditor1.QueryArea.SelectionStart
Var ml As Integer = Main.QueryEditor1.QueryArea.SelectionLength

Main.QueryEditor1.QueryArea.SelectionStart = 0
Main.QueryEditor1.QueryArea.SelectionLength = Main.QueryEditor1.QueryArea.Text.Length
Main.QueryEditor1.QueryArea.SelectionTextColor = Color.Black

Var s As String = Main.QueryEditor1.QueryArea.Text
Var l As Integer = s.Length-1

Var index2 As Integer
Var bluewords() As String = Array("SELECT","FROM","WHERE","CASE","AS ","WITH(nolock)","ON ","THEN","ELSE","DISTINCT","WHEN","CHAR","END","VARCHAR","WHERE","ORDER BY","GROUP BY")
Var graywords() As String = Array("OR ","IS ","NULL","=",">","<","+","-","AND","LIKE","NOT LIKE","IN ","NOT IN ","BETWEEN","INNER JOIN")
Var pinkwords() As String = Array("CAST","CONVERT","CHARINDEX","SUBSTRING","ISNULL")
For index1 As Integer = 0 To l
  For each pw as String in pinkwords
    index2 = s.IndexOf(index1, pw)
    If index2 = index1 Then
      Main.QueryEditor1.QueryArea.SelectionStart = index1
      Main.QueryEditor1.QueryArea.SelectionLength = pw.Length
      Main.QueryEditor1.QueryArea.SelectionTextColor = Color.Magenta
    End If
  Next
  For each gw as String in graywords
    index2 = s.IndexOf(index1, gw)
    If index2 = index1 Then
      Main.QueryEditor1.QueryArea.SelectionStart = index1
      Main.QueryEditor1.QueryArea.SelectionLength = gw.Length
      Main.QueryEditor1.QueryArea.SelectionTextColor = Color.Gray
    End If
  Next
  For each bw as String in bluewords
    index2 = s.IndexOf(index1, bw)
    If index2 = index1 Then
      Main.QueryEditor1.QueryArea.SelectionStart = index1
      Main.QueryEditor1.QueryArea.SelectionLength = bw.Length
      Main.QueryEditor1.QueryArea.SelectionTextColor = Color.Blue
    End If
  Next
Next

Main.QueryEditor1.QueryArea.Visible = TRUE

'Me.SetFocus
Main.QueryEditor1.QueryArea.SelectionStart = ms
Main.QueryEditor1.QueryArea.SelectionLength = ml

I would use RegEx to find the keywords, something like the following for the pinkwords (using the MBS RegEx class):

Var r as new RegExMBS
Var reg As String
Var start As Integer

r.CompileOptionMultiline = true
r.CompileOptionUngreedy = false
r.CompileOptionCaseLess = False

reg = "[CAST|CONVERT|CHARINDEX|SUBSTRING|ISNULL]"

If r.Compile(reg) = True Then
  while r.Execute(Main.QueryEditor1.QueryArea.Text, start) > 0
    Var p as Integer = r.OffsetCharacters(0) 
    Var l as Integer = r.OffsetCharacters(1)-r.OffsetCharacters(0)
    Main.QueryEditor1.QueryArea.StyledText.TextColor(p, l) = Color.Magenta
    start = r.Offset(1)
  Wend
End if

Thanks, I’ll look at this for optimizing what I currently have. Any thoughts on how I could make a string show in red?

For example how the ‘ID’ below is a dark red here. I want to do the same thing and change any text including the single quote to red.

SELECT CodeID as 'ID' 
FROM table

In a TextArea? Use styled text.

1 Like

Take a look here:

1 Like