|
-
Apr 12th, 2006, 03:14 PM
#1
Thread Starter
Hyperactive Member
[VS:03] RichText
Situation
I have a RichText Editor. I understand the RichText Box pretty well, and i know this can be done, but im not sure of the best was to do it.
I want to
I want to make a syntax highlighting system, so if the user types "Dim" (btw this isnt syntax highlighting for VB, its just an example), it turns the word dim blue.
Any help would be greatly appriciated.
-
Apr 12th, 2006, 03:39 PM
#2
Re: [VS:03] RichText
 Originally Posted by francisstokes
Situation
I have a RichText Editor. I understand the RichText Box pretty well, and i know this can be done, but im not sure of the best was to do it.
I want to
I want to make a syntax highlighting system, so if the user types "Dim" (btw this isnt syntax highlighting for VB, its just an example), it turns the word dim blue.
Any help would be greatly appriciated.
Hi,
Here's an example when the user types "d" , it turns all Characters turns blue.
VB Code:
Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(100) Then
RichTextBox1.ForeColor = Color.Blue
End If
End Sub
I think it's not exactly what you need but I think it's a start.
sparrow1
Last edited by sparrow1; Apr 12th, 2006 at 04:03 PM.
-
Apr 12th, 2006, 04:21 PM
#3
Thread Starter
Hyperactive Member
Re: [VS:03] RichText
Thank you, that is a point in the right direction .
I was thinking, using code similar to:
VB Code:
Private Sub rt_Check(...) Handles rtb.TextChanged
'code that checks through all the text to see if any keywords have been used
'and highlights them
End Sub
The code you gave me was good but had a major downfall:
1 -- If the user typed the word dim , it highlights ALL the text
-
Apr 12th, 2006, 04:23 PM
#4
Re: [VS:03] RichText
Yes, I know.
I thought that I mentioned in my post.
I'm still working on it right now, to solve it.
Wkr,
sparrow1
-
Apr 12th, 2006, 04:27 PM
#5
Thread Starter
Hyperactive Member
Re: [VS:03] RichText
I didnt mean it the way it looked. Sorry
-
Apr 12th, 2006, 04:51 PM
#6
Re: [VS:03] RichText
 Originally Posted by francisstokes
I didnt mean it the way it looked. Sorry 
No problem
In the mean time I found something more, loock at it:
VB Code:
Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
If e.KeyCode = Keys.D Then
RichTextBox1.SelectionColor = Color.Blue
Else
RichTextBox1.SelectionColor = Color.Black
End If
End Sub
It's better but still not at the end.
Wkr,
sparrow1
-
Apr 12th, 2006, 05:15 PM
#7
Thread Starter
Hyperactive Member
Re: [VS:03] RichText
Is there a way of searhing through the whole text for a specific word?
-
Apr 12th, 2006, 05:18 PM
#8
Re: [VS:03] RichText
 Originally Posted by francisstokes
Is there a way of searhing through the whole text for a specific word?
Oke francis,
I found the solution!
Here's the code:
VB Code:
Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
If e.KeyCode = Keys.D Or e.KeyCode = Keys.I Or e.KeyCode = Keys.M Then
RichTextBox1.SelectionColor = Color.Blue
Else
RichTextBox1.SelectionColor = Color.Black
End If
End Sub
Wkr,
sparrow1
-
Apr 12th, 2006, 05:44 PM
#9
Thread Starter
Hyperactive Member
Re: [VS:03] RichText
Ive got this code, but its not working the way it thought it would
VB Code:
'public vars
Public HighL(5) As String
'other relevent code
Sub highlight()
For i As Integer = 0 To 5
Dim iTextIndex As Integer
iTextIndex = rtb.Find(HighL(i))
If iTextIndex >= 0 Then
rtb.SelectionColor = Color.Blue
End If
Next i
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
HighL(0) = "Dim"
HighL(1) = "Public"
HighL(2) = "Sub"
HighL(3) = "Private"
HighL(4) = "ByVal"
HighL(5) = "MyBase"
End Sub
-
Apr 12th, 2006, 05:54 PM
#10
Re: [VS:03] RichText
I tried something like this in VB6. I don't know how much the RichTextBox has changed in .NET, but I bet you would have to modify the RTF code yourself.
Here is a look at the (incomplete) VB6 code that I was working on.
VB Code:
Private Sub ColorAllInputText()
' Declarations
Call LockWindowUpdate(txtInput.hWnd)
' Store the cursor start position
Call txtInput.GetSelection(lStart, lEnd)
sOpen = "{"
sSep = "$"
sClose = "}"
sNewLine = vbNullChar & "n" ' /n
sComStart = "/*"
sComEnd = "*/"
'sTextRTF = txtInput.TextRTF
sTextRTF = txtInput.Contents(SF_RTF)
' Add colors to the color table
lDefaultColor = AddColorToTable(sTextRTF, vbWindowText)
lOpenColor = AddColorToTable(sTextRTF, RGB(255, 0, 0)) ' And Close brace color
lDefNameColor = AddColorToTable(sTextRTF, RGB(0, 0, 255))
lDefValueColor = lDefaultColor
lSepColor = lOpenColor
lCommentColor = AddColorToTable(sTextRTF, RGB(0, 180, 0))
If Not mcFormatter.UseRealLineBreaks Then
lNLColor = lOpenColor 'AddColorToTable(sTextRTF, RGB(0, 128, 0))
End If
' Fix any possible errors caused by real text starting with "\cf"
sTextRTF = Replace$(sTextRTF, "\\", vbNullChar, , , vbTextCompare)
' Erase any old coloring
For i = 0 To 8
sTextRTF = Replace$(sTextRTF, "\cf" & i & " ", vbNullString, , , vbTextCompare)
sTextRTF = Replace$(sTextRTF, "\cf" & i, vbNullString, , , vbTextCompare)
Next i
' Handle all escaped characters
ReDim sToEscape(5)
sToEscape(0) = "\{"
sToEscape(1) = "$"
sToEscape(2) = "\}"
sToEscape(3) = "/n"
sToEscape(4) = "/*"
sToEscape(5) = "*/"
For i = 0 To UBound(sToEscape)
' Use vbnullchar because \\ has already been replaced with vbnullchar
sTextRTF = Replace$(sTextRTF, vbNullChar & sToEscape(i), Chr$(i + 1), , , vbTextCompare)
Next i
' Color the opening brace and the definiton name
sReplace = "\cf" & lOpenColor & "\" & sOpen & "\cf" & lDefNameColor & " "
sTextRTF = Replace$(sTextRTF, "\" & sOpen, sReplace, , , vbTextCompare)
' End the def name coloring by switching the separator color to its color
' and then color stuff after that as black.
sReplace = "\cf" & lSepColor & "\" & sSep & "\cf" & lDefValueColor & " "
sTextRTF = Replace$(sTextRTF, sSep, sReplace, , , vbTextCompare)
' Color the closing brace
sReplace = "\cf" & lOpenColor & "\" & sClose & "\cf" & lDefaultColor & " "
sTextRTF = Replace$(sTextRTF, "\" & sClose, sReplace, , , vbTextCompare)
' Color new line tokens
If Not mcFormatter.UseRealLineBreaks Then
' Use simply sNewLine because backslashes have already
' been dealt with when setting sNewLine
sReplace = "\cf" & lNLColor & sNewLine & "\cf" & lDefaultColor & " "
sTextRTF = Replace$(sTextRTF, sNewLine, sReplace, , , vbTextCompare)
End If
' Color comments last so it overwrites any coloring done inside the comment
' - Start comment:
sReplace = "\cf" & lCommentColor & " " & sComStart
sTextRTF = Replace$(sTextRTF, sComStart, sReplace, , , vbTextCompare)
' - End comment
sReplace = sComEnd & "\cf" & lDefaultColor & " "
sTextRTF = Replace$(sTextRTF, sComEnd, sReplace, , , vbTextCompare)
' Fix all escaped characters
For i = 0 To UBound(sToEscape)
sTextRTF = Replace$(sTextRTF, Chr$(i + 1), vbNullChar & sToEscape(i), , , vbTextCompare)
Next i
' Undo \\ fix
sTextRTF = Replace$(sTextRTF, vbNullChar, "\\", , , vbTextCompare)
txtInput.Contents(SF_RTF) = sTextRTF
Call txtInput.SetSelection(lStart, lEnd)
'txtInput.SelStart = lStart
Call LockWindowUpdate(0)
End Sub
Type some text into the RichTextBox and then have another textbox to hold this:
VB Code:
TextBox1.Text = RichTextBox1.RTF
That RTF text is what you will need to edit and learn about if you want this syntax highlighting to be efficient, and even then it is really hard to do when you have a lot of text.
-
Apr 12th, 2006, 06:46 PM
#11
Lively Member
Re: [VS:03] RichText
I have a calculator program that I have syntax highlighting as an user option. I use the following code. i found the basic and then modified it to fit my needs. It uses arrays of words.
VB Code:
#Region " Syntax Coloring RichtextBox1 "
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As Integer) As Integer
Dim FunctionColor As Color = Color.Blue
Dim UserVariableNameColor As Color = Color.DarkOrange
Dim OperatorColor As Color = Color.Red
Dim NumberColor As Color = Color.Black
Dim ConstantColor As Color = Color.LimeGreen
Public Enum EditMessages
LineIndex = 187
LineFromChar = 201
GetFirstVisibleLine = 206
CharFromPos = 215
PosFromChar = 1062
End Enum
Public Sub ColorRtb()
'Colors the entire RichTextbox
Dim FirstVisibleChar As Integer
Dim i As Integer = 0
While i < RichTextBox1.Lines.Length
FirstVisibleChar = GetCharFromLineIndex(i)
ColorLineNumber(i, FirstVisibleChar)
i += 1
End While
If TB.CanFocus Then TB.Focus()
End Sub
Public Sub ColorVisibleLines()
If SyntaxColoring Then
'Colors the visible lines in the RichTextbox
Dim FirstLine As Integer = FirstVisibleLine()
Dim LastLine As Integer = LastVisibleLine()
Dim FirstVisibleChar As Integer
Dim i As Integer = FirstLine
If (FirstLine = 0) And (LastLine = 0) Then
Exit Sub
Else
While i < LastLine
FirstVisibleChar = GetCharFromLineIndex(FirstLine)
ColorLineNumber(FirstLine, FirstVisibleChar)
FirstLine += 1
i += 1
End While
End If
End If
End Sub
Public Sub ColorLineNumber(ByVal LineIndex As Integer, ByVal lStart As Integer)
'Colors a single line in the RichTextBox control
Dim Line As String = RichTextBox1.Lines(LineIndex).ToLower
Dim i As Integer = 0
Dim j As Integer = 0
Dim k As Integer = 0
Dim SelectionAt As Integer = RichTextBox1.SelectionStart
Dim str As String
' Lock the update
LockWindowUpdate(RichTextBox1.Handle.ToInt32)
' Color the line black to remove any previous coloring
RichTextBox1.SelectionStart = lStart
RichTextBox1.SelectionLength = Line.Length
RichTextBox1.SelectionColor = Color.Black
'' Find any operators
i = 0
While i < strOp.Count
For j = 0 To Line.Length - 1
If Line.Substring(j, 1) = strOp(i).ToString Then
RichTextBox1.SelectionStart = (lStart + j)
RichTextBox1.SelectionLength = 1
RichTextBox1.SelectionColor = OperatorColor
End If
Next
i += 1
End While
'check for user defined variable names
If Names.Count > 0 Then
i = 0
While i < Names.Count
j = InStr(Line, CStr(Names(i)))
If j <> 0 Then
RichTextBox1.SelectionStart = (lStart + j - 1)
str = CStr(Names(i))
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = UserVariableNameColor
End If
i += 1
End While
End If
i = 0
'check for built in function names
While i < strFunc.Count
j = InStr(Line, CStr(strFunc(i)))
If j <> 0 Then
RichTextBox1.SelectionStart = (lStart + j - 1)
str = CStr(strFunc(i))
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = FunctionColor
End If
i += 1
End While
'check for built in constant names
i = 0
While i < StrConstant.Count
str = CStr(StrConstant(i)) 'get the constants name
j = 0 'reset the pointer
Do While j < Line.Length - 1 'search for the constant
j = Line.IndexOf(str, j) ' get the first instance of the constant
Select Case j
Case Is = -1 'constant not in line
Exit Do
Case Is = 0 'start of the string
'make sure that the constant is not the start of a word such as e in exp
If Line.Substring(j + 1, 1).ToLower Like "[a-z]" Then 'a letter preceeds
'not a constant
Else
RichTextBox1.SelectionStart = (lStart + j)
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = ConstantColor
End If
Case Else
Select Case (j + str.Length) = Line.Length
Case Is = True 'constant is at the end of the line
If Line.Substring(j - 1, 1).ToLower Like "[a-z]" Then 'a letter preceeds
'not a constant
Else
RichTextBox1.SelectionStart = (lStart + j)
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = ConstantColor
End If
Case Is = False 'the constant is somewhere within the string
If Line.Substring(j - 1, 1).ToLower Like "[a-z]" Then 'a letter preceeds
'not a constant
ElseIf Line.Substring(j + str.Length, 1).ToLower Like "[a-z]" Then 'a letter proceeds
'not a constant
Else
RichTextBox1.SelectionStart = (lStart + j)
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = ConstantColor
End If
End Select
End Select
j += str.Length
Loop
i += 1
End While
' Restore the selectionstart
RichTextBox1.SelectionStart = SelectionAt
RichTextBox1.SelectionLength = 0
' Unlock the update
LockWindowUpdate(0)
End Sub
Private Sub RTBTextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
ColorVisibleLines()
If TB.CanFocus Then TB.Focus()
End Sub
Public Function FirstVisibleLine() As Integer
'Uses API to determine the first line in a RichTextbox control
Return SendMessage(RichTextBox1.Handle.ToInt32, EditMessages.GetFirstVisibleLine, 0, 0)
End Function
Public Function LastVisibleLine() As Integer
'Returns the number of lines in a RichTextbox
Dim LastLine As Integer = CInt(FirstVisibleLine() + (RichTextBox1.Height / RichTextBox1.Font.Height))
If LastLine > RichTextBox1.Lines.Length Or LastLine = 0 Then
LastLine = RichTextBox1.Lines.Length
End If
Return LastLine
End Function
Public Function GetCharFromLineIndex(ByVal LineIndex As Integer) As Integer
' Uses API to get the character
Return SendMessage(RichTextBox1.Handle.ToInt32, EditMessages.LineIndex, LineIndex, 0)
End Function
#End Region
You may get something out of it.
-
May 16th, 2007, 08:56 PM
#12
Banned
Re: [VS:03] RichText
is not declared error list:
TB
SyntaxColoring
StrOp
Names
StrFunc
StrConstant
This is for the code posted above.
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
|