Real Time Syntax Highlightning aber wie?

  • VB.NET

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von ~Revolt~.

    Real Time Syntax Highlightning aber wie?

    Ich versuche die ganze Zeit ein erfolgreichen Syntax Highlighting zu bauen das nicht hängt
    Hat jemand eine Idee was an diese Code falsch sein könnte

    VB.NET-Quellcode

    1. Public Class SyntaxRTB
    2. Inherits System.Windows.Forms.RichTextBox
    3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    4. (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, _
    5. ByVal lParam As Integer) As Integer
    6. Private Declare Function LockWindowUpdate Lib "user32" _
    7. (ByVal hWnd As Integer) As Integer
    8. Private _SyntaxHighlight_CaseSensitive As Boolean = False
    9. 'Contains Windows Messages for the SendMessage API call
    10. Private Enum EditMessages
    11. LineIndex = 187
    12. LineFromChar = 201
    13. GetFirstVisibleLine = 206
    14. CharFromPos = 215
    15. PosFromChar = 1062
    16. End Enum
    17. Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
    18. ColorVisibleLines()
    19. End Sub
    20. Private Words As New DataTable
    21. Public Sub New()
    22. Dim MyRow As DataRow
    23. Dim arrKeyWords() As String, strKW As String
    24. Dim arrKeyWords2() As String, strKW2 As String
    25. Me.AcceptsTab = True
    26. ''Load all the keywords and the colors to make them
    27. Words.Columns.Add("Word")
    28. Words.PrimaryKey = New DataColumn() {Words.Columns(0)}
    29. Words.Columns.Add("Color")
    30. arrKeyWords = New String() {"Imports", "insert", "delete", _
    31. "truncate", "from", "where", "into", "inner", "update", _
    32. "outer", "on", "is", "declare", "set", "use", "values", "as", _
    33. "order", "by", "drop", "view", "go", "trigger", "cube", _
    34. "binary", "varbinary", "image", "char", "varchar", "text", _
    35. "datetime", "smalldatetime", "decimal", "numeric", "float", _
    36. "real", "bigint", "int", "smallint", "tinyint", "money", _
    37. "smallmoney", "bit", "cursor", "timestamp", "uniqueidentifier", _
    38. "sql_variant", "table", "nchar", "nvarchar", "ntext", "left", _
    39. "right", "like", "and", "all", "in", "null", "join", "not", "or"}
    40. arrKeyWords2 = New String() {"//", "'"}
    41. For Each strKW In arrKeyWords
    42. MyRow = Words.NewRow()
    43. MyRow("Word") = strKW
    44. MyRow("Color") = Color.Blue.Name
    45. Words.Rows.Add(MyRow)
    46. Next
    47. For Each strKW2 In arrKeyWords2
    48. MyRow = Words.NewRow()
    49. MyRow("Word") = strKW2
    50. MyRow("Color") = Color.Green.Name
    51. Words.Rows.Add(MyRow)
    52. Next
    53. End Sub
    54. Sub ColorRtb()
    55. Dim FirstVisibleChar As Integer
    56. Dim i As Integer = 0
    57. While i < Me.Lines.Length
    58. FirstVisibleChar = GetCharFromLineIndex(i)
    59. ColorLineNumber(i, FirstVisibleChar)
    60. i += 1
    61. End While
    62. End Sub
    63. Public Sub ColorVisibleLines()
    64. Dim FirstLine As Integer = FirstVisibleLine()
    65. Dim LastLine As Integer = LastVisibleLine()
    66. Dim FirstVisibleChar As Integer
    67. Dim i As Integer = FirstLine
    68. If (FirstLine = 0) And (LastLine = 0) Then
    69. ' If there is no text in the control, it will run an error
    70. ' So, if there isn't any text, just exit
    71. Exit Sub
    72. Else
    73. While i < LastLine
    74. FirstVisibleChar = GetCharFromLineIndex(FirstLine)
    75. ColorLineNumber(FirstLine, FirstVisibleChar)
    76. FirstLine += 1
    77. i += 1
    78. End While
    79. End If
    80. End Sub
    81. Public Sub ColorLineNumber(ByVal LineIndex As Integer, ByVal lStart As Integer)
    82. Dim i As Integer = 0
    83. Dim Instance As Integer = 0
    84. Dim LeadingChar = "", TrailingChar As String = ""
    85. Dim SelectionAt As Integer = Me.SelectionStart
    86. Dim MyRow As DataRow
    87. Dim Line() As String, MyI As Integer, MyStr As String
    88. ' Lock the update
    89. LockWindowUpdate(Me.Handle.ToInt32)
    90. MyI = lStart
    91. If CaseSensitive Then
    92. Line = Split(Me.Lines(LineIndex).ToString, " ")
    93. Else
    94. Line = Split(Me.Lines(LineIndex).ToLower, " ")
    95. End If
    96. For Each MyStr In Line
    97. Me.SelectionStart = MyI
    98. Me.SelectionLength = MyStr.Length
    99. If Words.Rows.Contains(MyStr) Then
    100. MyRow = Words.Rows.Find(MyStr)
    101. If (Not CaseSensitive) Or (CaseSensitive And MyRow("Word") = MyStr) Then
    102. Me.SelectionColor = Color.FromName(MyRow("Color"))
    103. End If
    104. Else
    105. Me.SelectionColor = Color.Black
    106. End If
    107. MyI += MyStr.Length + 1
    108. Next
    109. 'Kommentarsyntaxerkenner
    110. Dim LineKommentar As String = Me.Lines(LineIndex).ToLower
    111. Dim Kommentarzeichen1 As Integer
    112. Dim Kommentarzeichen2 As Integer
    113. 'Kommentare
    114. Kommentarzeichen1 = InStr(LineKommentar, "'")
    115. Kommentarzeichen2 = InStr(LineKommentar, "//")
    116. If Kommentarzeichen1 <> 0 Then
    117. Me.SelectionStart = (lStart + Kommentarzeichen1 - 1)
    118. Me.SelectionLength = (LineKommentar.Length - Kommentarzeichen1 + 1)
    119. Me.SelectionColor = Color.Green
    120. End If
    121. If Kommentarzeichen2 <> 0 Then
    122. Me.SelectionStart = (lStart + Kommentarzeichen2 - 1)
    123. Me.SelectionLength = (LineKommentar.Length - Kommentarzeichen2 + 1)
    124. Me.SelectionColor = Color.Green
    125. End If
    126. If Kommentarzeichen1 = 1 Then
    127. Me.SelectionStart = SelectionAt
    128. Me.SelectionLength = 0
    129. LockWindowUpdate(0)
    130. Exit Sub
    131. End If
    132. If Kommentarzeichen1 = 1 Then
    133. Me.SelectionStart = SelectionAt
    134. Me.SelectionLength = 0
    135. LockWindowUpdate(0)
    136. Exit Sub
    137. End If
    138. 'Position suchen
    139. Me.SelectionStart = SelectionAt
    140. Me.SelectionLength = 0
    141. Me.SelectionColor = Color.Black
    142. 'Update freigeben
    143. LockWindowUpdate(0)
    144. End Sub
    145. Public Function GetCharFromLineIndex(ByVal LineIndex As Integer) As Integer
    146. Return SendMessage(Me.Handle, EditMessages.LineIndex, LineIndex, 0)
    147. End Function
    148. Public Function FirstVisibleLine() As Integer
    149. Return SendMessage(Me.Handle, EditMessages.GetFirstVisibleLine, 0, 0)
    150. End Function
    151. Public Function LastVisibleLine() As Integer
    152. Dim LastLine As Integer = FirstVisibleLine() + (Me.Height / Me.Font.Height)
    153. If LastLine > Me.Lines.Length Or LastLine = 0 Then
    154. LastLine = Me.Lines.Length
    155. End If
    156. Return LastLine
    157. End Function
    158. Public Property CaseSensitive() As Boolean
    159. Get
    160. Return _SyntaxHighlight_CaseSensitive
    161. End Get
    162. Set(ByVal Value As Boolean)
    163. _SyntaxHighlight_CaseSensitive = Value
    164. End Set
    165. End Property
    166. End Class


    Wie gesagt diese Syntax RTB soll schnell laufen (Real time ohne zeitverzug) :)
    Danke im Voraus

    ...
    Keine dumme Kommentare!

    ~Revolt~ schrieb:

    dll = :thumbdown:

    Dann solltest Du zunächst Deine Meinung ändern und dann wieder im Forum nachfragen.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Hier meine Lösung^^

    VB.NET-Quellcode

    1. Imports System.Text.RegularExpressions
    2. Public Class SyntaxRTB
    3. Inherits System.Windows.Forms.RichTextBox
    4. #Region "Dateien"
    5. 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
    6. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As Integer) As Integer
    7. Private _SyntaxHighlight_CaseSensitive As Boolean = False
    8. Private Words As New DataTable
    9. Dim MyRow As DataRow
    10. Dim Kommentarzeichen1, Kommentarzeichen2 As Integer
    11. Dim Stringzeichen As Integer
    12. #End Region
    13. #Region "Windows API"
    14. Private Enum EditMessages
    15. LineIndex = 187
    16. LineFromChar = 201
    17. GetFirstVisibleLine = 206
    18. CharFromPos = 215
    19. PosFromChar = 1062
    20. End Enum
    21. #End Region
    22. #Region "Textchange-Event"
    23. Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
    24. ColorVisibleLines()
    25. End Sub
    26. #End Region
    27. #Region "Syntax Loader"
    28. Public Sub New()
    29. 'Wordloader Hier wird ein kleine Datenbank erstellt :P
    30. Words.Columns.Add("Word")
    31. Words.PrimaryKey = New DataColumn() {Words.Columns(0)}
    32. Words.Columns.Add("Color")
    33. 'Erste Syntaxreihe (Farbe blau)
    34. Dim Wordsblue() As String, KWblue As String
    35. Wordsblue = New String() {"Imports", "Module", "End", "Main", "Sub"}
    36. For Each KWblue In Wordsblue
    37. MyRow = Words.NewRow()
    38. MyRow("Word") = KWblue
    39. MyRow("Color") = Color.Blue.Name
    40. Words.Rows.Add(MyRow)
    41. Next
    42. 'Zweite Syntaxreihe (Farbe cyan)
    43. Dim Wordscyan() As String, KWcyan As String
    44. Wordscyan = New String() {"Program1", "App", "Main()", "Cmd"}
    45. For Each KWcyan In Wordscyan
    46. MyRow = Words.NewRow()
    47. MyRow("Word") = KWcyan
    48. MyRow("Color") = Color.DarkCyan.Name
    49. Words.Rows.Add(MyRow)
    50. Next
    51. End Sub
    52. #End Region
    53. #Region "Textcontroller"
    54. Public Sub ColorVisibleLines()
    55. Dim FirstLine As Integer = FirstVisibleLine()
    56. Dim LastLine As Integer = LastVisibleLine()
    57. Dim FirstVisibleChar As Integer
    58. Dim i As Integer = FirstLine
    59. 'Textkontroller (Falls Text vorhanden -> Färben sonst -> nichts)
    60. If (FirstLine = 0) And (LastLine = 0) Then
    61. Exit Sub
    62. Else
    63. While i < LastLine
    64. FirstVisibleChar = GetCharFromLineIndex(FirstLine)
    65. ColorLineNumber(FirstLine, FirstVisibleChar)
    66. FirstLine += 1
    67. i += 1
    68. End While
    69. End If
    70. End Sub
    71. #End Region
    72. Public Sub ColorLineNumber(ByVal LineIndex As Integer, ByVal lStart As Integer)
    73. Dim SelectionAt As Integer = Me.SelectionStart
    74. Dim Line() As String, MyI As Integer, MyStr As String
    75. 'Update verhindern
    76. LockWindowUpdate(Me.Handle.ToInt32)
    77. MyI = lStart
    78. If CaseSensitive Then 'Zeichen werden hier gesplittert (z. B. Main_Text statt Main_Text)
    79. Line = Split(Me.Lines(LineIndex).ToString, "_")
    80. Else
    81. Line = Split(Me.Lines(LineIndex).ToLower, "_")
    82. End If
    83. For Each MyStr In Line
    84. Me.SelectionStart = MyI
    85. Me.SelectionLength = MyStr.Length
    86. If Words.Rows.Contains(MyStr) Then
    87. MyRow = Words.Rows.Find(MyStr)
    88. If (Not CaseSensitive) Or (CaseSensitive And MyRow("Word") = MyStr) Then
    89. Me.SelectionColor = Color.FromName(MyRow("Color"))
    90. End If
    91. Else
    92. Me.SelectionColor = Color.Black
    93. End If
    94. MyI += MyStr.Length + 1
    95. Next
    96. 'Kommentarsyntaxerkenner
    97. Dim LineKommentar As String = Me.Lines(LineIndex).ToLower
    98. 'Kommentarzeichen
    99. Kommentarzeichen1 = InStr(LineKommentar, "'")
    100. Kommentarzeichen2 = InStr(LineKommentar, "//")
    101. If Kommentarzeichen1 <> 0 Then
    102. Me.SelectionStart = (lStart + Kommentarzeichen1 - 1)
    103. Me.SelectionLength = (LineKommentar.Length - Kommentarzeichen1 + 1)
    104. Me.SelectionColor = Color.Green
    105. End If
    106. If Kommentarzeichen2 <> 0 Then
    107. Me.SelectionStart = (lStart + Kommentarzeichen2 - 1)
    108. Me.SelectionLength = (LineKommentar.Length - Kommentarzeichen2 + 1)
    109. Me.SelectionColor = Color.Green
    110. End If
    111. 'Position suchen
    112. Me.SelectionStart = SelectionAt
    113. Me.SelectionLength = 0
    114. Me.SelectionColor = Color.Black
    115. 'Update freigeben
    116. LockWindowUpdate(0)
    117. End Sub
    118. Public Function GetCharFromLineIndex(ByVal LineIndex As Integer) As Integer
    119. Return SendMessage(Me.Handle, EditMessages.LineIndex, LineIndex, 0)
    120. End Function
    121. Public Function FirstVisibleLine() As Integer
    122. Return SendMessage(Me.Handle, EditMessages.GetFirstVisibleLine, 0, 0)
    123. End Function
    124. Public Function LastVisibleLine() As Integer
    125. Dim LastLine As Integer = FirstVisibleLine() + (Me.Height / Me.Font.Height)
    126. If LastLine > Me.Lines.Length Or LastLine = 0 Then
    127. LastLine = Me.Lines.Length
    128. End If
    129. Return LastLine
    130. End Function
    131. Public Property CaseSensitive() As Boolean
    132. Get
    133. Return _SyntaxHighlight_CaseSensitive
    134. End Get
    135. Set(ByVal Value As Boolean)
    136. _SyntaxHighlight_CaseSensitive = Value
    137. End Set
    138. End Property
    139. End Class


    Kleines Problem: Ich kann nur z. B Imports_System und nicht Imports.System da split nur ein Zeichen "wegsplittert" aber der Code ist perfekt
    wenn man unter Split(Me.Lines(LineIndex).ToString, "_") zu Split(Me.Lines(LineIndex).ToString, ".") ändert kann man es gut für VB.net verwenden^^ (Ausser strings und chars leider :/) <-daran wird gearbeitet

    VB.NET-Quellcode

    1. If CaseSensitive Then 'Zeichen werden hier gesplittert (z. B. Main_Text statt Main_Text)
    2. Line = Split(Me.Lines(LineIndex).ToString, ".")
    3. Else
    4. Line = Split(Me.Lines(LineIndex).ToLower, ".")
    5. End If

    Bedanken bitte