-
Dec 11th, 2006, 01:29 AM
#1
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
-
Dec 12th, 2006, 12:42 AM
#2
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
-
Dec 12th, 2006, 07:05 AM
#3
Re: Highlight syntax
Something like
VB Code:
Dim i As Long
With RichTextBox1
For i = 1 To Len(.Text)
If Mid(.Text, i, 1) = "{" Then
.SelStart = i
.SelLength = InStr(i, .Text, "}") - i - 1
.SelColor = vbBlue
.SelBold = True
End If
Next
End With
-
Dec 12th, 2006, 07:09 AM
#4
Re: Syntax Highlighter
Duplicate threads merged.
-
Dec 12th, 2006, 07:21 AM
#5
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
-
Dec 12th, 2006, 08:17 AM
#6
Re: Syntax Highlighter
Maybe this?
VB Code:
RichTextBox1 = Replace(RichTextBox1, "\{", "\b \{\b0 ")
RichTextBox1 = Replace(RichTextBox1, "\}", "\b \}\b0 ")
-
Dec 12th, 2006, 08:25 AM
#7
Re: Syntax Highlighter
Dear Andrew,
How to highlight with some colour?
Please mark you thread resolved using the Thread Tools as shown
-
Dec 12th, 2006, 09:01 AM
#8
Re: Syntax Highlighter
VB Code:
Private Sub Richtextbox1_Change()
'Get the current position in the text, this is for later as it changes when we add stuff
prevSel = Richtextbox1.SelStart
'We are messing around with the rtf tags. Here we replace \{, which is "{"
'with \cf1 \{\cf0 which is "[COLOR=Blue]{[/COLOR]"
rtxt = Replace(Richtextbox1.TextRTF, "\{", "\cf1 \{\cf0 ")
'Same with the closing tag
rtxt = Replace(rtxt, "\}", "\cf1 \}\cf0 ")
'Now for the colours to work, we need to add a certain tag at the begining,
'So we split the lines in the rtf, and insert it in. That way it can work.
prts = Split(rtxt, vbCrLf)
'Insert it in
prts(0) = prts(0) & vbCrLf & "{\colortbl ;\red0\green0\blue255;}"
'Now we have changed everything, so lets make the RTB display the changes
Richtextbox1.TextRTF = Join(prts, vbCrLf)
'Since we made changes, the cursor will change position, so we use the prevouse one we saved
Richtextbox1.SelStart = prevSel
End Sub
Last edited by Andrew G; Dec 13th, 2006 at 09:05 AM.
-
Dec 12th, 2006, 10:55 PM
#9
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
-
Dec 13th, 2006, 09:14 AM
#10
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
-
Dec 13th, 2006, 09:24 AM
#11
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
-
Dec 14th, 2006, 09:01 AM
#12
Re: Syntax Highlighter
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 "}"?
-
Dec 14th, 2006, 09:15 AM
#13
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
-
Dec 14th, 2006, 07:42 PM
#14
Re: Syntax Highlighter
VB Code:
Dim TagSelecting As Boolean
Private Sub rtb_Change()
Dim prevSel As Long
Dim RTxt As String
Dim Prts() As String
'If we're making deliberate changes from code, ie not the user, then we don't do anything
If TagSelecting Then Exit Sub
'Get the current position in the text, this is for later as it changes when we add stuff
prevSel = rtb.SelStart
'We are messing around with the rtf tags. Here we replace \{, which is "{"
'with \cf1 \{\cf0 which is "{"
RTxt = Replace(rtb.TextRTF, "\{", "\cf1 \{\cf0 ")
'Same with the closing tag
RTxt = Replace(RTxt, "\}", "\cf1 \}\cf0 ")
'Now for the colours to work, we need to add a certain tag at the begining,
'So we split the lines in the rtf, and insert it in. That way it can work.
Prts = Split(RTxt, vbCrLf)
'Insert it in
Prts(0) = Prts(0) & vbCrLf & "{\colortbl ;\red0\green0\blue255;}"
'Now we have changed everything, so lets make the RTB display the changes
rtb.TextRTF = Join(Prts, vbCrLf)
'Since we made changes, the cursor will change position, so we use the prevouse one we saved
rtb.SelStart = prevSel
End Sub
Private Sub rtb_KeyPress(KeyAscii As Integer)
Dim Prts() As String
Dim x As Integer
Dim tot As Long
Dim Lne As String
Dim BeNum As Long
Dim EnNum As Long
'Check if all tags are present
If KeyAscii = vbKeyReturn Then
'We're messing around again, so better not change anything
TagSelecting = True
'We need to find out the line we are just about to leave, so we split them up
Prts = Split(rtb.Text, vbCrLf)
'Loop through them
For x = 0 To UBound(Prts)
'Tot is the total characters so far, now if the total is greater than our current
'posistion, means that it was this line
tot = tot + Len(Prts(x)) + Len(vbCrLf)
'Woot we found it :)
If tot > rtb.SelStart Then Exit For
Next
'Now we only need this line
Lne = Prts(x)
'Go through it and find all begining and closing tags
For x = 1 To Len(Lne)
If Mid(Lne, x, 1) = "{" Then
BeNum = BeNum + 1 'Count them up
ElseIf Mid(Lne, x, 1) = "}" Then
EnNum = EnNum + 1 'Count them up
End If
Next
'Display appropriate messages
If BeNum > EnNum Then
MsgBox "Missing closing tags!"
KeyAscii = 0
ElseIf BeNum < EnNum Then
MsgBox "Missing begining tags!"
KeyAscii = 0
End If
'Done :)
TagSelecting = False
End If
End Sub
Private Sub rtb_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
'Cause when we select something, make sure all prevous red tags are now blue
rtb_Change
End Sub
Private Sub rtb_SelChange()
Dim PrevPos As Long
Dim PrevLen As Long
Dim Endpos As Long
Dim BeginPos As Long
If TagSelecting Then Exit Sub
If Trim(rtb.SelText) = "{" Then
'We are going to change stuff, so we'll make sure we don't run the code in RTB_change
TagSelecting = True
'Our current selection so we can select it later
PrevPos = rtb.SelStart
PrevLen = rtb.SelLength
'Colour our tag red
rtb.SelColor = vbRed
'Find the other end tag
Endpos = InStr(rtb.SelStart + 1, rtb.Text, "}")
'Highlight it and colour it red
If Not Endpos = 0 Then
rtb.SelStart = Endpos - 1
rtb.SelLength = 1
rtb.SelColor = vbRed
End If
'Reselect our prevous selection
rtb.SelStart = PrevPos
rtb.SelLength = PrevLen
'Done :)
TagSelecting = False
ElseIf Trim(rtb.SelText) = "}" Then
'We're changing stuff, so don't run the change code
TagSelecting = True
'Our current selection so we can select it later
PrevPos = rtb.SelStart
PrevLen = rtb.SelLength
'Colour it red
rtb.SelColor = vbRed
'Find our begining tag
BeginPos = Len(rtb.Text) - InStr(Len(rtb.Text) - rtb.SelStart, StrReverse(rtb.Text), "{")
'If we found the begining tag colour it in
If Not BeginPos = 0 Then
rtb.SelStart = BeginPos
rtb.SelLength = 1
rtb.SelColor = vbRed
End If
'Return our previous selection
rtb.SelStart = PrevPos
rtb.SelLength = PrevLen
'Done :)
TagSelecting = False
End If
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.
-
Dec 15th, 2006, 07:30 AM
#15
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
-
Dec 17th, 2006, 03:38 AM
#16
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.
-
Jan 10th, 2007, 10:47 PM
#17
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
-
Jan 11th, 2007, 06:33 AM
#18
Re: Syntax Highlighter
You can put this into the command button
VB Code:
Dim lns() As String
Dim l As Integer
Dim OpenNum As Long
Dim CloseNum As Long
Dim StrMsg As String
lns = Split(rtb.Text, vbCrLf)
For l = 0 To UBound(lns)
OpenNum = 0
CloseNum = 0
Found = InStr(1, lns(l), "{")
Do While Found
OpenNum = OpenNum + 1
Found = InStr(Found + 1, lns(l), "{")
Loop
Found = InStr(1, lns(l), "}")
Do While Found
CloseNum = CloseNum + 1
Found = InStr(Found + 1, lns(l), "}")
Loop
If OpenNum <> CloseNum Then
StrMsg = StrMsg & "line " & l + 1 & vbCrLf
End If
Next
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|