Solved

SQL code highlighting in Richtextbox

Posted on 2002-06-12
8
3,162 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

617 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