Results 1 to 8 of 8

Thread: [2005] Syntax Highliting in TextBox

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    [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

  2. #2
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    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.

  3. #3

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    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

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Syntax Highliting in TextBox


  5. #5

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

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

  6. #6
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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"}

  7. #7

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    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

  8. #8
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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
  •  



Click Here to Expand Forum to Full Width