Results 1 to 12 of 12

Thread: [2005] Syntax highting in ritch text box

  1. #1

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    [2005] Syntax highting in ritch text box

    How can I make all keywords in a ritch textbox have automatic color set to blue or green ect...
    I intend to have a list of a keyword structure...
    Code:
    <Serializable()>Freind Structure KeyWord
         Public WordColor as Color
         Public WordBold as Boolean
         Public WordUnderLine as Boolean 
         Public WordItalic as boolean
         Public Word as String
    End Structure
    I know how to format selected text but thats it...

    Any help would be appreciated...

    Signatures suck

  2. #2
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [2005] Syntax highting in ritch text box

    automatic color ? do you mean just setting the
    RichTextBox1.Forecolor = Color.Blue

  3. #3

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] Syntax highting in ritch text box

    No thats not what i mean, I mean setting the font color of specific words, in real time...
    For example dim, public.
    just words I have defined

    Signatures suck

  4. #4
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [2005] Syntax highting in ritch text box

    i am going to test this out myself, but the theory i have to do is to search through the text box every character that is typed, or maybe every time they hit the space bar, and then go through the list, and find the word instance, then find the first character, the length, select the text, then highlight it..

    easier said than done at this point.

  5. #5
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [2005] Syntax highting in ritch text box

    I got it to highlight the word and change the color, but when i continue it sets the cursor to the front of the text, anyone know how to switch this up and put it at the end ?


    -----------------------------
    RTB.SelctionStart = position
    Last edited by NPassero; Jun 28th, 2007 at 07:56 AM. Reason: resolved.

  6. #6

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] Syntax highting in ritch text box

    The ritch text box has a method to get and set the position the carrot(i think thats what its called)
    I was thinking something pretty much like what you suggest, but only watch the text at the position of the carrot back to the last space and upto the next space. eg:
    start = index of first space char before carrot
    end = index of first space char after carrot
    get the string between those two indexes and compare in for loop to the words in list, if word matches then get keywords formatting and apply to the chars between start and end...

    Signatures suck

  7. #7
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [2005] Syntax highting in ritch text box

    the way i have it now..

    vb.net Code:
    1. Dim s As String
    2.       Dim word As String = "the"
    3.       Dim i As Integer
    4.  
    5.       s = rtb.Text
    6.  
    7.       i = s.IndexOf(word, last)
    8.  
    9.       If i > -1 Then
    10.          rtb.Select(i, word.Length)
    11.          rtb.SelectionColor = Color.Blue
    12.          rtb.DeselectAll()                        '< ----- added still does not work.
    13.          rtb.SelectionStart = s.Length
    14.          last = s.Length
    15.       End If


    Now the problem i am having is that the rest of the text is
    changing to blue, even if i add this:

    rtb.ForeColor = Color.Black

    It shouldn't change the rest of the text color should it ?
    Last edited by NPassero; Jun 28th, 2007 at 08:15 AM.

  8. #8
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [2005] Syntax highting in ritch text box

    I got this code from post #5 of this thread
    http://vbforums.com/showthread.php?t...tbox+highlight

    I just hacked away at this for about ten minutes to get it to do two different sets of words with different colors.
    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
    Private BlueWords 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
    If BlueWords.Rows.Contains(MyStr) Then
    MyRow = BlueWords.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
    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
    Dim arrBlueKeyWords() 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")

    BlueWords.Columns.Add("Word")
    BlueWords.PrimaryKey = New DataColumn() {BlueWords.Columns(0)}
    BlueWords.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"}

    arrBlueKeyWords = New String() {"the", "second", "group", "of", "words"}

    For Each strKW In arrKeyWords
    MyRow = Words.NewRow()
    MyRow("Word") = strKW
    MyRow("Color") = Color.LightCoral.Name
    Words.Rows.Add(MyRow)
    Next

    For Each strKW In arrBlueKeyWords
    MyRow = BlueWords.NewRow()
    MyRow("Word") = strKW
    MyRow("Color") = Color.LawnGreen.Name
    BlueWords.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
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  9. #9
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [2005] Syntax highting in ritch text box

    I got mine working, a little simpler ( only works with one word right now, gotta change that )

    needed to add this..
    vb Code:
    1. rtb.SelectionColor = Color.Black
    after i was done with the selection

    * does not work when you delete

  10. #10

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] Syntax highting in ritch text box

    How do I get and set the position of the caret?

    Signatures suck

  11. #11
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [2005] Syntax highting in ritch text box

    RTB.SelectionStart = StartPosition

  12. #12

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] Syntax highting in ritch text box

    One way would be to store the position of each space charector in order, and do this...
    Code:
    'Semi sudo code
    dim index as integer = 0
    for each i as integer in intSpaceCharIndexes
       rtb.selectionStart = index
       rtb.selectionEnd = i
       if KeyWord(rtb.SelectedText) then
          rtb.selectedText.color = blue
       end if
       index = i
    next

    Signatures suck

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