This last weekend I expirimented a little bit with extending the functionality of the RichTextBox control. Below you'll find an example of a small class that enherits from the RichTextBox control and allows you to implement syntax highlighting (with the use of a couple Win32 API call to smooth over the process of course.) The code pretty much speaks for itself.
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
End Class
Currently rated 3.3 by 4 people - Currently 3.25/5 Stars.
- 1
- 2
- 3
- 4
- 5
Brian Tinkler is out in Redmond this weekend where he get to be one of the 5 judges for the national competition for software design for the Imagine Cup. This is awesome!
He is blogging about his experiences out there:
- First Impressions from Redmond: http://geekswithblogs.net/btinkler/archive/2005/05/20/40095.aspx
- He got to meet Scoble: http://geekswithblogs.net/btinkler/archive/2005/05/20/40146.aspx
- 2Go Services: http://geekswithblogs.net/btinkler/archive/2005/05/21/40156.aspx
- VS2005, then "Orkus", then "Hawaii": http://geekswithblogs.net/btinkler/archive/2005/05/21/40157.aspx
The Wisconsin .NET User Group and the Wisconsin development community in general is Representin'!
Be the first to rate this post - Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
I ordered Visual Studio 2005 Beta 2 about 2 weeks ago, and I received it in the mail yesterday! The SQL Server 2005 CTP cam with it! I couldn't wait to install them and start playing. I didn't play around much with Beta 1; I just didn't make the point to spend much time with it (There are so many things to learn/do I can hardly find the time for it all.) One of the reasons I'm so excited about Beta 2 is because of the Go Live license (which you must go onlin and sign first; located here: http://lab.msdn.microsoft.com/vs2005/golive/). And so my journey began...
I uninstalled VS2005 Beta 1 and .NET Framework 2.0 Beta 1. Then I realized I forgot to uninstall Microsoft Visual J# .NET Redistributable Package 2.0 Beta, so I then uninstalled that. Then I started the Install of SQL Server 2005 CTP and go the following error:
Errors occurred during the installation: Beta components detected. Error 50266 installing .NET Framework 2.0
I then went and found that there were still 2.0 files on the drive and registry entries for them. So I made a copy of the files and registry entries (for backup) and then deleted the originals trying to manually uninstall the remaining .NET 2.0 Beta 1 components. To make a long story short, this didn't work so I had to restore the copies of these files.
After all that (took like 4 hours, I think) I searched Google and found this page: http://lab.msdn.microsoft.com/vs2005/uninstall
Apparently there is an exact order to uninstall the Beta 1 components in order to be able to install Beta 2. The part I screwed up with was I uninstalled the J# Redistributable Package 2.0 Beta 1 after I uninstalled .NET Framework 2.0 Beta 1.
Opps! I began wondering if I would need to reinstall Windows in order to get those bits off my machine so I could install Beta 2. Well, further down the page I saw this:
Notes: 1. If you see an erro removing J# .NET Redistributable Package 2.0 from Add/Remove Programs, please run “msiexec /x {9046F10C-F5E7-4871-BED9-8288F19C70DF}” from a command line window. 1. If you see an error removing .NET Framework 2.0 from Add/Remove Programs, please run “msiexec /x {71F8EFBF-09AF-418D-91F1-52707CDFA274}” from a command line window.
Wahoo! Exactly what I needed. I ran the part at the command lin to uninstall J# .NET Redistributable Package 2.0 and it worked like a charm. I am now successfully installing SQL Server 2005 CTP and Visual Studio 2005 Beta 2 while I type this post.
Be the first to rate this post - Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
|