Results 1 to 18 of 18

Thread: Syntax Highlighter

  1. #1

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Syntax Highlighter

    Dear All,
    I want to Build a Syntex Highlighting Editor with Richtextbox.That it should search for opening of a tag like "'{" and ending for the
    same.If any ending or starting tag is missing it has to higlighted.I need a method with DLL. The richtext box is hanging if the text is more
    than 5000 line.How to do that
    Please mark you thread resolved using the Thread Tools as shown

  2. #2

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Highlight syntax

    Dear All,
    I want to check for opening and closing of "{" tag in a Richtext box.I have to highlight the missing tag with some colour
    in a speeder way.The richtext box is having arround 1000 line.How can I?
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Highlight syntax

    Something like
    VB Code:
    1. Dim i As Long
    2.  
    3.     With RichTextBox1
    4.         For i = 1 To Len(.Text)
    5.             If Mid(.Text, i, 1) = "{" Then
    6.                 .SelStart = i
    7.                 .SelLength = InStr(i, .Text, "}") - i - 1
    8.                 .SelColor = vbBlue
    9.                 .SelBold = True
    10.             End If
    11.         Next
    12.     End With

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Syntax Highlighter

    Duplicate threads merged.

  5. #5

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Syntax Highlighter

    Dear Hack,
    Thanks for your reply.It is highlighting the total line.How to higlight only the ending and closing Tag?
    Please mark you thread resolved using the Thread Tools as shown

  6. #6
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Syntax Highlighter

    Maybe this?
    VB Code:
    1. RichTextBox1 = Replace(RichTextBox1, "\{", "\b \{\b0 ")
    2. RichTextBox1 = Replace(RichTextBox1, "\}", "\b \}\b0 ")

  7. #7

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Syntax Highlighter

    Dear Andrew,
    How to highlight with some colour?
    Please mark you thread resolved using the Thread Tools as shown

  8. #8
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Syntax Highlighter

    VB Code:
    1. Private Sub Richtextbox1_Change()
    2.     'Get the current position in the text, this is for later as it changes when we add stuff
    3.     prevSel = Richtextbox1.SelStart
    4.     'We are messing around with the rtf tags. Here we replace \{, which is "{"
    5.     'with \cf1 \{\cf0  which is "[COLOR=Blue]{[/COLOR]"
    6.     rtxt = Replace(Richtextbox1.TextRTF, "\{", "\cf1 \{\cf0 ")
    7.     'Same with the closing tag
    8.     rtxt = Replace(rtxt, "\}", "\cf1 \}\cf0 ")
    9.    
    10.     'Now for the colours to work, we need to add a certain tag at the begining,
    11.     'So we split the lines in the rtf, and insert it in. That way it can work.
    12.     prts = Split(rtxt, vbCrLf)
    13.     'Insert it in
    14.     prts(0) = prts(0) & vbCrLf & "{\colortbl ;\red0\green0\blue255;}"
    15.     'Now we have changed everything, so lets make the RTB display the changes
    16.     Richtextbox1.TextRTF = Join(prts, vbCrLf)
    17.    
    18.     'Since we made changes, the cursor will change position, so we use the prevouse one we saved
    19.     Richtextbox1.SelStart = prevSel
    20. End Sub
    Last edited by Andrew G; Dec 13th, 2006 at 09:05 AM.

  9. #9

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Syntax Highlighter

    Dear Andrew,
    Thanks for your reply. It is Highlighting the Opening tag in blue color.But what about the closing tag. And also I will be happy , if
    it is commented.
    Please mark you thread resolved using the Thread Tools as shown

  10. #10
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Syntax Highlighter

    The closing tag was the commented out code in the previous post. Either way, i just edited it, and added the comments and made it work with the closing tags

  11. #11

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Syntax Highlighter

    Dear AndrewG,
    Thanks for Your Reply.How can I Highlight the missing "}" in red colour. And also how to higlight the missing tag in different colour in command click event.
    Please mark you thread resolved using the Thread Tools as shown

  12. #12
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Syntax Highlighter

    Quote Originally Posted by danasegarane
    Dear AndrewG,
    Thanks for Your Reply.How can I Highlight the missing "}" in red colour. And also how to higlight the missing tag in different colour in command click event.
    Could you explain a bit more. What do you mean by missing "}"?

  13. #13

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Syntax Highlighter

    Dear Andrew,
    Thanks for your reply.If the sentence is
    "This for making { in the proper }, and{ Blabl "
    In this end "}" is missing
    I want to do the following
    1.when the cursor reaches the any tag,if it is a open tag then,it should highlight the appropriate end tag and vice versa
    2. Give a messsage box higlighting the first missing tag in a line

    Am I clear?
    Please mark you thread resolved using the Thread Tools as shown

  14. #14
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Syntax Highlighter

    VB Code:
    1. Dim TagSelecting As Boolean
    2.  
    3. Private Sub rtb_Change()
    4. Dim prevSel As Long
    5. Dim RTxt As String
    6. Dim Prts() As String
    7.  
    8. 'If we're making deliberate changes from code, ie not the user, then we don't do anything
    9. If TagSelecting Then Exit Sub
    10.     'Get the current position in the text, this is for later as it changes when we add stuff
    11.     prevSel = rtb.SelStart
    12.     'We are messing around with the rtf tags. Here we replace \{, which is "{"
    13.     'with \cf1 \{\cf0  which is "{"
    14.     RTxt = Replace(rtb.TextRTF, "\{", "\cf1 \{\cf0 ")
    15.     'Same with the closing tag
    16.     RTxt = Replace(RTxt, "\}", "\cf1 \}\cf0 ")
    17.  
    18.     'Now for the colours to work, we need to add a certain tag at the begining,
    19.     'So we split the lines in the rtf, and insert it in. That way it can work.
    20.     Prts = Split(RTxt, vbCrLf)
    21.     'Insert it in
    22.     Prts(0) = Prts(0) & vbCrLf & "{\colortbl ;\red0\green0\blue255;}"
    23.     'Now we have changed everything, so lets make the RTB display the changes
    24.     rtb.TextRTF = Join(Prts, vbCrLf)
    25.  
    26.     'Since we made changes, the cursor will change position, so we use the prevouse one we saved
    27.     rtb.SelStart = prevSel
    28. End Sub
    29.  
    30. Private Sub rtb_KeyPress(KeyAscii As Integer)
    31. Dim Prts() As String
    32. Dim x As Integer
    33. Dim tot As Long
    34. Dim Lne As String
    35. Dim BeNum As Long
    36. Dim EnNum As Long
    37.  
    38. 'Check if all tags are present
    39. If KeyAscii = vbKeyReturn Then
    40.     'We're messing around again, so better not change anything
    41.     TagSelecting = True
    42.     'We need to find out the line we are just about to leave, so we split them up
    43.     Prts = Split(rtb.Text, vbCrLf)
    44.     'Loop through them
    45.     For x = 0 To UBound(Prts)
    46.         'Tot is the total characters so far, now if the total is greater than our current
    47.         'posistion, means that it was this line
    48.         tot = tot + Len(Prts(x)) + Len(vbCrLf)
    49.         'Woot we found it :)
    50.         If tot > rtb.SelStart Then Exit For
    51.     Next
    52.    
    53.     'Now we only need this line
    54.     Lne = Prts(x)
    55.    
    56.     'Go through it and find all begining and closing tags
    57.     For x = 1 To Len(Lne)
    58.         If Mid(Lne, x, 1) = "{" Then
    59.             BeNum = BeNum + 1   'Count them up
    60.         ElseIf Mid(Lne, x, 1) = "}" Then
    61.             EnNum = EnNum + 1   'Count them up
    62.         End If
    63.     Next
    64.    
    65.     'Display appropriate messages
    66.     If BeNum > EnNum Then
    67.         MsgBox "Missing closing tags!"
    68.         KeyAscii = 0
    69.     ElseIf BeNum < EnNum Then
    70.         MsgBox "Missing begining tags!"
    71.         KeyAscii = 0
    72.     End If
    73.  
    74.     'Done :)
    75.     TagSelecting = False
    76. End If
    77. End Sub
    78.  
    79. Private Sub rtb_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    80.     'Cause when we select something, make sure all prevous red tags are now blue
    81.     rtb_Change
    82. End Sub
    83.  
    84. Private Sub rtb_SelChange()
    85. Dim PrevPos As Long
    86. Dim PrevLen As Long
    87. Dim Endpos As Long
    88. Dim BeginPos As Long
    89.  
    90. If TagSelecting Then Exit Sub
    91.     If Trim(rtb.SelText) = "{" Then
    92.         'We are going to change stuff, so we'll make sure we don't run the code in RTB_change
    93.         TagSelecting = True
    94.         'Our current selection so we can select it later
    95.         PrevPos = rtb.SelStart
    96.         PrevLen = rtb.SelLength
    97.        
    98.         'Colour our tag red
    99.         rtb.SelColor = vbRed
    100.         'Find the other end tag
    101.         Endpos = InStr(rtb.SelStart + 1, rtb.Text, "}")
    102.         'Highlight it and colour it red
    103.         If Not Endpos = 0 Then
    104.             rtb.SelStart = Endpos - 1
    105.             rtb.SelLength = 1
    106.             rtb.SelColor = vbRed
    107.         End If
    108.        
    109.         'Reselect our prevous selection
    110.         rtb.SelStart = PrevPos
    111.         rtb.SelLength = PrevLen
    112.        
    113.         'Done :)
    114.         TagSelecting = False
    115.     ElseIf Trim(rtb.SelText) = "}" Then
    116.         'We're changing stuff, so don't run the change code
    117.         TagSelecting = True
    118.         'Our current selection so we can select it later
    119.         PrevPos = rtb.SelStart
    120.         PrevLen = rtb.SelLength
    121.    
    122.         'Colour it red
    123.         rtb.SelColor = vbRed
    124.         'Find our begining tag
    125.         BeginPos = Len(rtb.Text) - InStr(Len(rtb.Text) - rtb.SelStart, StrReverse(rtb.Text), "{")
    126.        
    127.         'If we found the begining tag colour it in
    128.         If Not BeginPos = 0 Then
    129.             rtb.SelStart = BeginPos
    130.             rtb.SelLength = 1
    131.             rtb.SelColor = vbRed
    132.         End If
    133.                
    134.         'Return our previous selection
    135.         rtb.SelStart = PrevPos
    136.         rtb.SelLength = PrevLen
    137.        
    138.         'Done :)
    139.         TagSelecting = False
    140.     End If
    141. End Sub
    It only highlights in red if the person highlights one of the tags. Also if there are nested tags {string1 {string2} string 3} then the tag selecting won't work properly.
    Last edited by Andrew G; Dec 14th, 2006 at 07:57 PM.

  15. #15

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Syntax Highlighter

    Dear AndrewG,
    Thanks for Kind Reply. There is no need to give a message box when pressing the enter key.While pressing any key like
    check tag match it has to higlight the missing tag.And when the cursor moves to any {,} tag , it has to highlight the beging and end tag with
    some different colour.How do I
    Please mark you thread resolved using the Thread Tools as shown

  16. #16
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Syntax Highlighter

    When you highlight any of the tags, it will highlight the other one. It will take a bit more time to make it work on the cursor instead of highlight. Also i made it so that when you press enter like in VB, it brings up the message and prevents you from moving to another line. You could add if someone presses the up or down key, and on the mouseup event to check if they went to a different line.

  17. #17

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Syntax Highlighter

    Dear Andrew,
    Is there any method , by calling this function is a clicke event.That is check for opening/closing tag , in a selected(if selected,else in the full content of a richtext),Highlight the first missing opening/closing Tag?
    Please mark you thread resolved using the Thread Tools as shown

  18. #18
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Syntax Highlighter

    You can put this into the command button
    VB Code:
    1. Dim lns() As String
    2. Dim l As Integer
    3. Dim OpenNum As Long
    4. Dim CloseNum As Long
    5. Dim StrMsg As String
    6.  
    7.     lns = Split(rtb.Text, vbCrLf)
    8.    
    9. For l = 0 To UBound(lns)
    10.     OpenNum = 0
    11.     CloseNum = 0
    12.  
    13.     Found = InStr(1, lns(l), "{")
    14.     Do While Found
    15.         OpenNum = OpenNum + 1
    16.         Found = InStr(Found + 1, lns(l), "{")
    17.     Loop
    18.    
    19.     Found = InStr(1, lns(l), "}")
    20.     Do While Found
    21.         CloseNum = CloseNum + 1
    22.         Found = InStr(Found + 1, lns(l), "}")
    23.     Loop
    24.    
    25.     If OpenNum <> CloseNum Then
    26.         StrMsg = StrMsg & "line " & l + 1 & vbCrLf
    27.     End If
    28. Next
    29.  
    30. If Len(StrMsg) Then MsgBox "You are missing tags on:" & StrMsg

    You might have to tweak it a bit, its a bit late here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width