Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL code highlighting in Richtextbox

Posted on 2002-06-12
8
Medium Priority
?
3,231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

705 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