|
-
Apr 29th, 2007, 12:27 PM
#1
Thread Starter
WiggleWiggle
[2005] Syntax Highliting in TextBox
I want to create a text editor where the user will be able to download files/add-ons that will highlight the code in their file.
I want it to be a file/add-on so that it is not hard coded into the thing and i can charge for them and so on...
How would be the best way to go about this? Would i need to use a richtextbox?
My usual boring signature: Something
-
Apr 29th, 2007, 12:29 PM
#2
Frenzied Member
Re: [2005] Syntax Highliting in TextBox
Yes, you would need to use a richtextbox.
One way to do it is using a keyword file for each language you want highlighted.
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
May 1st, 2007, 07:00 PM
#3
Thread Starter
WiggleWiggle
Re: [2005] Syntax Highliting in TextBox
ok i have the rest of the project done, except for the syntax highlighting. Does any one know what i should do?
My usual boring signature: Something
-
May 1st, 2007, 07:08 PM
#4
Re: [2005] Syntax Highliting in TextBox
-
May 2nd, 2007, 11:23 PM
#5
Thread Starter
WiggleWiggle
Re: [2005] Syntax Highliting in TextBox
i got the code from the post above this one. Can some one tell me how it knows what code to highlight?
vb Code:
Public Class SyntaxRTB
Inherits System.Windows.Forms.RichTextBox
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As Integer) As Integer
Private _SyntaxHighlight_CaseSensitive As Boolean = False
Private Words As New DataTable
'Contains Windows Messages for the SendMessage API call
Private Enum EditMessages
LineIndex = 187
LineFromChar = 201
GetFirstVisibleLine = 206
CharFromPos = 215
PosFromChar = 1062
End Enum
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
ColorVisibleLines()
End Sub
Public Sub ColorRtb()
Dim FirstVisibleChar As Integer
Dim i As Integer = 0
While i < Me.Lines.Length
FirstVisibleChar = GetCharFromLineIndex(i)
ColorLineNumber(i, FirstVisibleChar)
i += 1
End While
End Sub
Public Sub ColorVisibleLines()
Dim FirstLine As Integer = FirstVisibleLine()
Dim LastLine As Integer = LastVisibleLine()
Dim FirstVisibleChar As Integer
If (FirstLine = 0) And (LastLine = 0) Then
'If there is no text it will error, so exit the sub
Exit Sub
Else
While FirstLine < LastLine
FirstVisibleChar = GetCharFromLineIndex(FirstLine)
ColorLineNumber(FirstLine, FirstVisibleChar)
FirstLine += 1
End While
End If
End Sub
Public Sub ColorLineNumber(ByVal LineIndex As Integer, ByVal lStart As Integer)
Dim i As Integer = 0
Dim Instance As Integer
Dim LeadingChar, TrailingChar As String
Dim SelectionAt As Integer = Me.SelectionStart
Dim MyRow As DataRow
Dim Line() As String, MyI As Integer, MyStr As String
' Lock the update
LockWindowUpdate(Me.Handle.ToInt32)
MyI = lStart
If CaseSensitive Then
Line = Split(Me.Lines(LineIndex).ToString, " ")
Else
Line = Split(Me.Lines(LineIndex).ToLower, " ")
End If
For Each MyStr In Line
Me.SelectionStart = MyI
Me.SelectionLength = MyStr.Length
If Words.Rows.Contains(MyStr) Then
MyRow = Words.Rows.Find(MyStr)
If (Not CaseSensitive) Or (CaseSensitive And MyRow("Word") = MyStr) Then
Me.SelectionColor = Color.FromName(MyRow("Color"))
End If
Else
Me.SelectionColor = Color.Black
End If
MyI += MyStr.Length + 1
Next
' Restore the selectionstart
Me.SelectionStart = SelectionAt
Me.SelectionLength = 0
Me.SelectionColor = Color.Black
' Unlock the update
LockWindowUpdate(0)
End Sub
Public Function GetCharFromLineIndex(ByVal LineIndex As Integer) As Integer
Return SendMessage(Me.Handle, EditMessages.LineIndex, LineIndex, 0)
End Function
Public Function FirstVisibleLine() As Integer
Return SendMessage(Me.Handle, EditMessages.GetFirstVisibleLine, 0, 0)
End Function
Public Function LastVisibleLine() As Integer
Dim LastLine As Integer = FirstVisibleLine() + (Me.Height / Me.Font.Height)
If LastLine > Me.Lines.Length Or LastLine = 0 Then
LastLine = Me.Lines.Length
End If
Return LastLine
End Function
Public Sub New()
Dim MyRow As DataRow
Dim arrKeyWords() As String, strKW As String
Me.AcceptsTab = True
''Load all the keywords and the colors to make them
Words.Columns.Add("Word")
Words.PrimaryKey = New DataColumn() {Words.Columns(0)}
Words.Columns.Add("Color")
arrKeyWords = New String() {"select", "insert", "delete", _
"truncate", "from", "where", "into", "inner", "update", _
"outer", "on", "is", "declare", "set", "use", "values", "as", _
"order", "by", "drop", "view", "go", "trigger", "cube", _
"binary", "varbinary", "image", "char", "varchar", "text", _
"datetime", "smalldatetime", "decimal", "numeric", "float", _
"real", "bigint", "int", "smallint", "tinyint", "money", _
"smallmoney", "bit", "cursor", "timestamp", "uniqueidentifier", _
"sql_variant", "table", "nchar", "nvarchar", "ntext", "left", _
"right", "like","and","all","in","null","join","not","or"}
For Each strKW In arrKeyWords
MyRow = Words.NewRow()
MyRow("Word") = strKW
MyRow("Color") = Color.LightCoral.Name
Words.Rows.Add(MyRow)
Next
End Sub
Public Property CaseSensitive() As Boolean
Get
Return _SyntaxHighlight_CaseSensitive
End Get
Set(ByVal Value As Boolean)
_SyntaxHighlight_CaseSensitive = Value
End Set
End Property
My usual boring signature: Something
-
May 3rd, 2007, 01:10 PM
#6
Re: [2005] Syntax Highliting in TextBox
See the array called "arrKeyWords"
Code:
arrKeyWords = New String() {"select", "insert", "delete",
_ "truncate", "from", "where", "into", "inner", "update",
_ "outer", "on", "is", "declare", "set", "use", "values", "as",
_ "order", "by", "drop", "view", "go", "trigger", "cube",
_ "binary", "varbinary", "image", "char", "varchar", "text",
_ "datetime", "smalldatetime", "decimal", "numeric", "float",
_ "real", "bigint", "int", "smallint", "tinyint", "money",
_ "smallmoney", "bit", "cursor", "timestamp", "uniqueidentifier",
_ "sql_variant", "table", "nchar", "nvarchar", "ntext", "left",
_ "right", "like","and","all","in","null","join","not","or"}
-
May 3rd, 2007, 05:34 PM
#7
Thread Starter
WiggleWiggle
Re: [2005] Syntax Highliting in TextBox
So i put the codes (words) to be highlighed in that array and they will all be the same color? I kinda wannted more then that... like say
<a href="someURL">Blah</a>
My usual boring signature: Something
-
May 3rd, 2007, 07:17 PM
#8
Re: [2005] Syntax Highliting in TextBox
You could create multiple arrays for different keywords which can be displayed in different colors. In order to do this you'll need to write some code, everything you need is present in that example, just extend it.
The color used for each string is set here;
Code:
For Each strKW In arrKeyWords
MyRow = Words.NewRow()
MyRow("Word") = strKW
MyRow("Color") = Color.LightCoral.Name
Words.Rows.Add(MyRow)
Next
If you want more elaborate highlighting, like tags in HTML, then you will need to use regular expressions.
Last edited by Bulldog; May 3rd, 2007 at 07:28 PM.
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
|