![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
Syntax Highlighting
Ive looked everywhere for this, ive downloaded 3 examples, and I cant find anything useful. The only thing I found was written in C# and I couldnt figure out how to translate it to VB.NET because it was written oddly. (http://www.c-sharpcorner.com/Code/20...hTextBoxP2.asp)
Anyways, I was wondering how to do this. I have 2 arrays, strHighlight(), and strColor() strHighlight contains what to highlight, and strColor contains what color to highlight that item. The way I was thinking of doing this is, when you click a line, or change text, the function finds the line the cursor is at, loops through everything in strHighlight for matching values, highlights with whatever the loop count is using strColor(). Then updates the whole line with the color, and replaces your cursor where it was before. I have not a clue how to write it at all, since I have nearly no experience with Rich Text Boxes, or doing things like this... can someone give me a few properties that will be helpful and what they do? |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
Alright ive taken my best stab at this, and came up with
Public Function rtbTextBoxSyntaxUpdate()
Dim i As Integer
Dim iSelectStart As Integer
Dim iSelectEnd As Integer
Dim iCurrentLine As Integer
Dim strLine() As String
iCurrentLine = SendMessage(rtbTextBox.Handle.ToInt32(), EM_LINEFROMCHAR, -1, 0) + 1
iSelectStart = rtbTextBox.SelectionStart
strLine = Split(rtbTextBox.Lines(iCurrentLine - 1), " ", -1, CompareMethod.Text)
For i = 0 To UBound(strLine)
If strLine(i) = "Hello" Then
rtbTextBox.SelectionColor = System.Drawing.Color.Blue
End If
Next
rtbTextBox.Select(iSelectStart, rtbTextBox.SelectionLength)
End FunctionWhat it does is when you type Hello it will change the text color to blue. A could of things wrong, one it doesnt highlight Hello blue, it changes the font, in whats theyped next, and theres not an array of highlights or colors yet. So what I cant figure out is how to actually select the word that the cursor is in, so then rtbTextBox.SelectionColor = whatever, will actually work properly and highlight the word... any ideas? |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
I have it working
Dim i As Integer = 1
Dim ii As Integer
Dim bMatch As Boolean = False
Dim iPosition As Integer
Dim iSelectStart As Integer = 0
Dim iSelectedPositionStart As Integer
'Dim iSelectEnd As Integer
Dim iCurrentLine As Integer
Dim strLine() As String
'Define Highlight array
Dim strHighlight() As String = {"echo", "print", "include", "fopen", "fclose", "function", "array", "true", "false"}
'Define numbers
iCurrentLine = SendMessage(rtbTextBox.Handle.ToInt32(), EM_LINEFROMCHAR, -1, 0) + 1
iSelectedPositionStart = rtbTextBox.SelectionStart
'Find position of first character on current line
While i < iCurrentLine
iSelectStart = iSelectStart + rtbTextBox.Lines(i - 1).Length + 1
i = i + 1
End While
'Change line color to black
rtbTextBox.Select(iSelectStart, rtbTextBox.Lines(iCurrentLine - 1).Length)
rtbTextBox.SelectionColor = System.Drawing.Color.Black
i = 0
strLine = Split(rtbTextBox.Lines(iCurrentLine - 1), " ", -1, CompareMethod.Text)
For i = 0 To UBound(strLine)
For ii = 0 To UBound(strHighlight)
If strHighlight(ii) = strLine(i) Then
bMatch = True
End If
Next
If bMatch = True Then
rtbTextBox.Select(iSelectStart + iPosition, strLine(i).Length)
rtbTextBox.SelectionColor = System.Drawing.Color.Blue
End If
rtbTextBox.Select(iSelectedPositionStart, 0)
rtbTextBox.SelectionColor = System.Drawing.Color.Black
iPosition = iPosition + strLine(i).Length + 1
bMatch = False
Next
End FunctionIts a piece of junk, i can hardly even understand what I was doing in some places because its so disorganized, but it works. It updates the current line every time a character has been changed with new systax highlighting, 1. Changes whole line black (default typing color) by selecting it and changing select color to black. 2. Searches each individual word (the split will soon be a regex to highlight functions and strings and integers), if it finds a match gets its position, selects the word, changes select word to blue (soon to be customizable colors). 3. Its done The problem is the line flickers every time you type a letter, by that I mean you can see the whole line selected and each matching word selected for a split second... other syntax highlighters dont do that. Any way I can fix this? |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
Sorry for all the multiple posts but I cant edit the posts, anyways, ill post one last thing, since the more testing I do the more problems im finding.
The problem its having is with the regex, since when its split the regex contains a space, a tab, and several single characters ({}()[]). The highlight position is getting off because of that. The regular expression only splits the line by the character, it doesnt leave in what it was split by, so I cant add the size of the tab, or space, or single character to it correctly, and its causing a lot of problems, the count is getting off even if one of those exists and the highlight fails... is there any way I can split and leave in the split key or something? or any way to find out what it was split with? [code] Dim rexRegEx As New Regex("([{}();])") strLine = rexRegEx.Split(rtbTextBox.Lines(iCurrentLine - 1)) [code] Also I had an idea to prevent the flicker, which is eaisly noticable. Is it possible for me to create a "Dummy" rich text box and apply the highlight to that, then make the original rtb equal to that? or something like that? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|