Solved

SQL code highlighting in Richtextbox

Posted on 2002-06-12
8
2,950 Views
Last Modified: 2008-05-04
Hi all,

Is it possible to highlight code in a richtextbox in the same way as in the SQL Query analyzer?
I've looked in MSDN, and I think it's feasable, but I really didn't find how to do that.

Thanks for the help
0
Comment
Question by:Catouch
  • 4
  • 2
  • 2
8 Comments
 
LVL 4

Expert Comment

by:gencross
ID: 7072564
Here is a very basic example of hightlighting in a rich text box

rText.Text = "Don't highlight, highlight"
   
rText.SelStart = 17
rText.SelLength = 9


Hope it helps.
0
 
LVL 4

Expert Comment

by:gencross
ID: 7072569
Note, There are other properties that set attribs. for the selected text such as Color, bold, etc...  They all begin with Sel (SelColor) in the properties of the Rich text box.

0
 
LVL 1

Author Comment

by:Catouch
ID: 7072572
I know how to higlight, thanks.
I meant the SQL highlighting format.
eg: when you write "exec", it automatically turns to blue...

SQL syntax in higlighted in different colors.
Is this possible?

Thanks
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7072573
Do you mean that you want to use an RTB with SQL code and highlight it as SQL would? If so then I can certainly help here as I was involved in another thread that did exactly this a while ago.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:Catouch
ID: 7072585
yes, that's it!
0
 
LVL 1

Author Comment

by:Catouch
ID: 7072586
yes, that's it!
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 7072591
Ok, just found it:

Add a textbox and a richtextbox on the form, add a reference to Microsoft SQL Parser Object Library 1.0 (SQLPARSE.DLL in \program files\microsoft office\office\ folder) Then run the application and start typing in the text box, the RTB should display the correctly highlighted SQL code.

Private Sub WriteToRTF(ByRef RTBox As RichTextBox, ByVal Statement As String)
    Dim intElement As Long
    RTBox.Text = ""
    Dim aryElements() As String
    aryElements = Split(Statement, "\cf ")
    For intElement = LBound(aryElements) To UBound(aryElements)
        If Trim(aryElements(intElement)) <> "" Then
'            If aryElements(intElement) = String(Len(aryElements(intElement)), " ") Then
'                RTBox.Text = RTBox.Text & aryElements(intElement)
'            Else
                strContent = Mid(Trim(aryElements(intElement)), 6, Len(Trim(aryElements(intElement))) - 5)
                With RTBox
                    .Text = .Text & String(Len(aryElements(intElement)) - Len(strContent) - 5, " ")
                    .Text = .Text & strContent
                    .SelStart = Len(.Text) - Len(strContent)
                    .SelLength = Len(strContent)
'                    Debug.Print .SelStart, .SelLength, strContent
                    Select Case Mid(Trim(aryElements(intElement)), 4, 2)
                    Case "1 "
                        'Keyword
                        .SelColor = vbBlue
                    Case "3 "
                        'System Function
                        .SelColor = vbGreen
                    Case "5 "
                        'Operator
                        .SelColor = RGB(75, 75, 75)
                    Case "6 "
                        'Text
                        .SelColor = vbRed
                    Case "7 "
                        'Number
                        .SelColor = vbCyan
                    Case "8 "
                        'Comment
                        .SelColor = vbMagenta
                    Case "10"
                        'Standard Text
                        .SelColor = vbBlack
                    Case Else '2, 4, 9
                    End Select
                End With
'            End If
        End If
    Next
End Sub

Private Sub Text1_Change()
    ParseContent Text1.Text
End Sub

Private Sub ParseContent(ByVal SQL As String)
   Dim osQL As MSSQLParser.vbSQLParser
   Dim sTemp As String
   Set osQL = New MSSQLParser.vbSQLParser
   sTemp = osQL.ParseSQLSyntax(SQL, vbSqlServerSyntax)
   WriteToRTF RichTextBox1, sTemp
   Debug.Print sTemp
   Set osQL = Nothing
End Sub
0
 
LVL 1

Author Comment

by:Catouch
ID: 7072602
thanks a lot
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now