RichTextBox-Färbung (Syntax-Highlighting)

  • VB.NET

Es gibt 8 Antworten in diesem Thema. Der letzte Beitrag () ist von Devilx1.

    RichTextBox-Färbung (Syntax-Highlighting)

    Hallo,
    ich habe eine RichTextBox. In diese soll der User Befehle eingeben. Diese sollen dann gehighlightet werden (BLAU, Großgeschrieben). Das Problem: Wenn der User den Befehl eingibt, kann dieser klein/gemischt geschrieben sein. Trotzdem soll der Befehl erkannt werden und blau gefärbt und großgeschreiben werden.

    Hat jemand ne IDEE?

    Peter

    PS: Wie realisiere ich die Blaufärbung und Großschreibung?

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „mikeb69“ ()

    OK.
    Ich hab mich bemüht und folgenden Code gefunden:

    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. Friend Words As New DataTable
    10. 'Contains Windows Messages for the SendMessage API call
    11. Private Enum EditMessages
    12. LineIndex = 187
    13. LineFromChar = 201
    14. GetFirstVisibleLine = 206
    15. CharFromPos = 215
    16. PosFromChar = 1062
    17. End Enum
    18. Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
    19. MyBase.OnTextChanged(e)
    20. ColorVisibleLines()
    21. End Sub
    22. Public Sub New()
    23. Me.AcceptsTab = True
    24. AddSQLSyntax()
    25. End Sub
    26. Function AddSQLSyntax()
    27. ClearSyntaxWords()
    28. AddSyntaxWord("\b(select|text|ntext|date|datetime|order by|" & _
    29. "group by|smalldatetime|cursor|on|as|for|filename|" & _
    30. "database|drop|function|delete|insert|update|int|" & _
    31. "varchar|nvarchar|bit|binary|table|inner|where|from|" & _
    32. "out|procedure|view|trigger|set)\b", Color.Blue)
    33. AddSyntaxWord("\b@@identity\b", Color.Pink)
    34. AddSyntaxWord("\b(in|join|outer|and|or)\b", Color.Gray)
    35. AddSyntaxWord("\bsp_refreshview\b", Color.Red)
    36. Return True
    37. End Function
    38. Public Function ClearSyntaxWords()
    39. Words = New DataTable
    40. ''Load all the keywords and the colors to make them
    41. Words.Columns.Add("Word")
    42. Words.PrimaryKey = New DataColumn() {Words.Columns(0)}
    43. Words.Columns.Add("Color")
    44. Return True
    45. End Function
    46. Public Function AddSyntaxWord(ByVal strWord As String, ByVal clrColor As Color)
    47. Dim MyRow As DataRow
    48. MyRow = Words.NewRow()
    49. MyRow("Word") = strWord
    50. MyRow("Color") = clrColor.Name
    51. Words.Rows.Add(MyRow)
    52. Return True
    53. End Function
    54. Public 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. If (FirstLine = 0) And (LastLine = 0) Then
    68. 'If there is no text it will error, so exit the sub
    69. Exit Sub
    70. Else
    71. While FirstLine < LastLine
    72. FirstVisibleChar = GetCharFromLineIndex(FirstLine)
    73. ColorLineNumber(FirstLine, FirstVisibleChar)
    74. FirstLine += 1
    75. End While
    76. End If
    77. End Sub
    78. Public Sub ColorLineNumber(ByVal LineIndex As Integer, ByVal lStart As Integer)
    79. Dim i As Integer = 0
    80. Dim SelectionAt As Integer = Me.SelectionStart
    81. Dim MyRow As DataRow
    82. Dim MyI As Integer
    83. ' Lock the update
    84. LockWindowUpdate(Me.Handle.ToInt32)
    85. MyI = lStart
    86. ''Turn the whole link black before applying RegEx Syntax matching.
    87. Me.SelectionStart = MyI
    88. Me.SelectionLength = Lines(LineIndex).Length
    89. Me.SelectionColor = Color.Black
    90. ''Check for matches in a particular line number
    91. Dim rm As System.Text.RegularExpressions.MatchCollection
    92. Dim m As System.Text.RegularExpressions.Match
    93. For Each MyRow In Words.Rows
    94. '"( |^)1.*2( |$)"
    95. rm = System.Text.RegularExpressions.Regex.Matches(Me.Text, MyRow("Word"))
    96. For Each m In rm
    97. Me.SelectionStart = m.Index
    98. Me.SelectionLength = m.Length
    99. Me.SelectionColor = Color.FromName(MyRow("color"))
    100. Next
    101. Next
    102. ' Restore the selectionstart
    103. Me.SelectionStart = SelectionAt
    104. Me.SelectionLength = 0
    105. Me.SelectionColor = Color.Black
    106. ' Unlock the update
    107. LockWindowUpdate(0)
    108. End Sub
    109. Public Function GetCharFromLineIndex(ByVal LineIndex As Integer) As Integer
    110. Return SendMessage(Me.Handle, EditMessages.LineIndex, LineIndex, 0)
    111. End Function
    112. Public Function FirstVisibleLine() As Integer
    113. Return SendMessage(Me.Handle, EditMessages.GetFirstVisibleLine, 0, 0)
    114. End Function
    115. Public Function LastVisibleLine() As Integer
    116. Dim LastLine As Integer = FirstVisibleLine() + (Me.Height / Me.Font.Height)
    117. If LastLine > Me.Lines.Length Or LastLine = 0 Then
    118. LastLine = Me.Lines.Length
    119. End If
    120. Return LastLine
    121. End Function
    122. Public Property CaseSensitive() As Boolean
    123. Get
    124. Return _SyntaxHighlight_CaseSensitive
    125. End Get
    126. Set(ByVal Value As Boolean)
    127. _SyntaxHighlight_CaseSensitive = Value
    128. End Set
    129. End Property
    130. End Class

    Aus pietschsoft.com/download/blog/1311/SyntaxRTB.vb.txt

    Doch wie rufe ich die Funktion auf?

    Peter
    Frage hat sich geklärt:

    VB.NET-Quellcode

    1. Dim rtb_Main As New SyntaxRTB
    2. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3. With rtb_Main
    4. .Dock = DockStyle.Fill
    5. .CaseSensitive = False
    6. End With
    7. Me.Controls.Add(rtb_Main)
    8. End Sub


    Keine Doppelposts.
    Kein Hochpuschen eigener Beiträge.
    Gruss
    mikeb69

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „mikeb69“ ()