|
-
Dec 19th, 2006, 10:29 AM
#1
Thread Starter
Fanatic Member
Syntax Highlighter
I was wondering if it was possible to have a richtextbox be used as a program that highlights syntaxs.
For example:
Completed Syntaxs:
<b>Hello!</b>
<i>Hello!</i>
<u><b>Hello!</b></u>
^^^
If it's completed then it does this color
<b>Hello!
<u>Hello!
^^^
If not then it does another color
<br>
<hr>
^^^
And finally tags that don't require a closing tag are set to another color.
Could somebody please give me some ideas on how to get started with this... or a commented working example?
I'm making a program for some friends who want to start to learn HTML (Myspace is their motivation, lol).
-
Dec 19th, 2006, 11:49 AM
#2
Addicted Member
Re: Syntax Highlighter
Yes, I have used a RichTextBox in order to highlight code. Keep in mind that you will have to write code that actually examines the contents of the RTB and then actually applies the color to the appropriate text items...!
-
Dec 19th, 2006, 12:09 PM
#3
Thread Starter
Fanatic Member
Re: Syntax Highlighter
Uhhh, how exactly would i go about doing this? I only really know basically what needs to be done, but there's two ways i can think of doing it.
The first step is to search the entire richtextbox for certain strings, such as the <br>, then either replacing that with a special code that changes the color, or by selecting the text (the <br> text) & changing the properties.
But for the rest of what i've asked such as showing an incomplete tag, i really have no idea =(.
-
Dec 20th, 2006, 09:17 AM
#4
Addicted Member
Re: Syntax Highlighter
...no mystery, since you already see what needs to be done!
Yes, you've got to use either INSTR on the RTB.TEXT, or use the intrinsic FIND method of the RTB to find the tag pairs you want. Then set the .SelStart and SelLength appropriately. Then set the .SelColor and/or SelBold and/or SelItalic (well, you get the idea) and then continue until you reach the end of the .TEXT itself.
Note that you do not need to REPLACE the <br> or other tags unless this suits you. You can leave them as they are, if you wish. If you want to get rid of them, be aware that you may have a problem; using VB's REPLACE function affects the .TEXT and will, by itself, cause the previous color highlighting to be removed (a Catch 22, for sure).
So, as you see, this requires a fair bit of work...
-
Dec 20th, 2006, 11:02 AM
#5
Thread Starter
Fanatic Member
Re: Syntax Highlighter
I run into a few problems when i try this though
Here's pretty much where i'm up to:
VB Code:
Public Sub Highlight_Single_Tags(Textbox_ As RichTextBox)
Dim i As Long
With Textbox_
For i = 1 To Len(.Text)
If Mid(.Text, i, 4) = "<br>" Then
.SelStart = i - 1
.SelLength = 4
.SelColor = vbBlue
.SelLength = 0
End If
If Mid(.Text, i, 3) = "<p>" Then
.SelStart = i - 1
.SelLength = 3
.SelColor = vbRed
.SelLength = 0
End If
Next
End With
End Sub
Now, the problem with this way is that it moves the cursor around. So once it checks for the tags it will move the cursor back to the start of the tag, this can be annoying. Not only that, it stops you from typing after that because i want it to update the colors after every keypress, so every time you press a key, the cursor will jump back to the start of the last tag it changed.
I don't want it to do that, i just want it to replace the color of the text without moving the cursor around. I don't know how to do this =(. Especially since it will be looking for multiple tags that can show up a number of times.
Also removing the colors of the affected text when it is no longer a tag will be hard.
And this is just for stand alone tags... This is going to be difficult.
Any ideas/examples?
Last edited by Slyke; Dec 20th, 2006 at 11:33 AM.
-
Dec 21st, 2006, 02:36 PM
#6
Thread Starter
Fanatic Member
-
Dec 21st, 2006, 03:22 PM
#7
Addicted Member
Re: Syntax Highlighter
...well, you probably want to limit the events which cause syntax highlighting to occur when the user presses an logical word seperator (such as SPACE or TAB or ENTER, etc).
And even then you can capture the .SelStart before using it and then reset it after the syntax highlighting code completes...?
And the FOR...NEXT loop you show below can be much improved by the use of another variable, such as Y.
VB Code:
Y = 1
DO
X = INSTR(Y,TEXT,"<br>")
IF X = 0 THEN EXIT DO
...other code to perform highlighting...
Y = X + 1
LOOP
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
|