Results 1 to 12 of 12

Thread: [VS:03] RichText

  1. #1

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    [VS:03] RichText

    Situation

    I have a RichText Editor. I understand the RichText Box pretty well, and i know this can be done, but im not sure of the best was to do it.

    I want to

    I want to make a syntax highlighting system, so if the user types "Dim" (btw this isnt syntax highlighting for VB, its just an example), it turns the word dim blue.

    Any help would be greatly appriciated.

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [VS:03] RichText

    Quote Originally Posted by francisstokes
    Situation

    I have a RichText Editor. I understand the RichText Box pretty well, and i know this can be done, but im not sure of the best was to do it.

    I want to

    I want to make a syntax highlighting system, so if the user types "Dim" (btw this isnt syntax highlighting for VB, its just an example), it turns the word dim blue.

    Any help would be greatly appriciated.
    Hi,

    Here's an example when the user types "d" , it turns all Characters turns blue.


    VB Code:
    1. Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
    2.         If e.KeyChar = Microsoft.VisualBasic.ChrW(100) Then
    3.                     RichTextBox1.ForeColor = Color.Blue
    4.                 End If
    5.      
    6.        
    7.     End Sub

    I think it's not exactly what you need but I think it's a start.

    sparrow1
    Last edited by sparrow1; Apr 12th, 2006 at 04:03 PM.
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: [VS:03] RichText

    Thank you, that is a point in the right direction .
    I was thinking, using code similar to:

    VB Code:
    1. Private Sub rt_Check(...) Handles rtb.TextChanged
    2.          'code that checks through all the text to see if any keywords have been used
    3.          'and highlights them
    4.     End Sub

    The code you gave me was good but had a major downfall:
    1 -- If the user typed the word dim , it highlights ALL the text

  4. #4
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [VS:03] RichText

    Yes, I know.
    I thought that I mentioned in my post.
    I'm still working on it right now, to solve it.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  5. #5

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: [VS:03] RichText

    I didnt mean it the way it looked. Sorry

  6. #6
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [VS:03] RichText

    Quote Originally Posted by francisstokes
    I didnt mean it the way it looked. Sorry
    No problem

    In the mean time I found something more, loock at it:

    VB Code:
    1. Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
    2.         If e.KeyCode = Keys.D Then
    3.  
    4.             RichTextBox1.SelectionColor = Color.Blue
    5.         Else
    6.             RichTextBox1.SelectionColor = Color.Black
    7.         End If
    8.     End Sub

    It's better but still not at the end.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  7. #7

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: [VS:03] RichText

    Is there a way of searhing through the whole text for a specific word?

  8. #8
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [VS:03] RichText

    Quote Originally Posted by francisstokes
    Is there a way of searhing through the whole text for a specific word?
    Oke francis,

    I found the solution!
    Here's the code:

    VB Code:
    1. Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
    2.         If e.KeyCode = Keys.D Or e.KeyCode = Keys.I Or e.KeyCode = Keys.M Then
    3.  
    4.             RichTextBox1.SelectionColor = Color.Blue
    5.         Else
    6.             RichTextBox1.SelectionColor = Color.Black
    7.         End If
    8.  
    9.     End Sub

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  9. #9

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: [VS:03] RichText

    Ive got this code, but its not working the way it thought it would

    VB Code:
    1. 'public vars
    2.  Public HighL(5) As String
    3.  
    4. 'other relevent code
    5.  
    6.     Sub highlight()
    7.  
    8.         For i As Integer = 0 To 5
    9.  
    10.             Dim iTextIndex As Integer
    11.             iTextIndex = rtb.Find(HighL(i))
    12.             If iTextIndex >= 0 Then
    13.                 rtb.SelectionColor = Color.Blue
    14.             End If
    15.  
    16.         Next i
    17.  
    18.     End Sub
    19.  
    20.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    21.         HighL(0) = "Dim"
    22.         HighL(1) = "Public"
    23.         HighL(2) = "Sub"
    24.         HighL(3) = "Private"
    25.         HighL(4) = "ByVal"
    26.         HighL(5) = "MyBase"
    27.     End Sub

  10. #10
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [VS:03] RichText

    I tried something like this in VB6. I don't know how much the RichTextBox has changed in .NET, but I bet you would have to modify the RTF code yourself.

    Here is a look at the (incomplete) VB6 code that I was working on.
    VB Code:
    1. Private Sub ColorAllInputText()
    2.     ' Declarations
    3.    
    4.     Call LockWindowUpdate(txtInput.hWnd)
    5.     ' Store the cursor start position
    6.     Call txtInput.GetSelection(lStart, lEnd)
    7.  
    8.     sOpen = "{"
    9.     sSep = "$"
    10.     sClose = "}"
    11.     sNewLine = vbNullChar & "n"    ' /n
    12.     sComStart = "/*"
    13.     sComEnd = "*/"
    14.  
    15.     'sTextRTF = txtInput.TextRTF
    16.     sTextRTF = txtInput.Contents(SF_RTF)
    17.  
    18.     ' Add colors to the color table
    19.     lDefaultColor = AddColorToTable(sTextRTF, vbWindowText)
    20.     lOpenColor = AddColorToTable(sTextRTF, RGB(255, 0, 0))           ' And Close brace color
    21.     lDefNameColor = AddColorToTable(sTextRTF, RGB(0, 0, 255))
    22.     lDefValueColor = lDefaultColor
    23.     lSepColor = lOpenColor
    24.     lCommentColor = AddColorToTable(sTextRTF, RGB(0, 180, 0))
    25.     If Not mcFormatter.UseRealLineBreaks Then
    26.         lNLColor = lOpenColor  'AddColorToTable(sTextRTF, RGB(0, 128, 0))
    27.     End If
    28.  
    29.     ' Fix any possible errors caused by real text starting with "\cf"
    30.     sTextRTF = Replace$(sTextRTF, "\\", vbNullChar, , , vbTextCompare)
    31.    
    32.     ' Erase any old coloring
    33.     For i = 0 To 8
    34.         sTextRTF = Replace$(sTextRTF, "\cf" & i & " ", vbNullString, , , vbTextCompare)
    35.         sTextRTF = Replace$(sTextRTF, "\cf" & i, vbNullString, , , vbTextCompare)
    36.     Next i
    37.  
    38.     ' Handle all escaped characters
    39.     ReDim sToEscape(5)
    40.     sToEscape(0) = "\{"
    41.     sToEscape(1) = "$"
    42.     sToEscape(2) = "\}"
    43.     sToEscape(3) = "/n"
    44.     sToEscape(4) = "/*"
    45.     sToEscape(5) = "*/"
    46.     For i = 0 To UBound(sToEscape)
    47.         ' Use vbnullchar because \\ has already been replaced with vbnullchar
    48.         sTextRTF = Replace$(sTextRTF, vbNullChar & sToEscape(i), Chr$(i + 1), , , vbTextCompare)
    49.     Next i
    50.  
    51.     ' Color the opening brace and the definiton name
    52.     sReplace = "\cf" & lOpenColor & "\" & sOpen & "\cf" & lDefNameColor & " "
    53.     sTextRTF = Replace$(sTextRTF, "\" & sOpen, sReplace, , , vbTextCompare)
    54.  
    55.     ' End the def name coloring by switching the separator color to its color
    56.     ' and then color stuff after that as black.
    57.     sReplace = "\cf" & lSepColor & "\" & sSep & "\cf" & lDefValueColor & " "
    58.     sTextRTF = Replace$(sTextRTF, sSep, sReplace, , , vbTextCompare)
    59.  
    60.     ' Color the closing brace
    61.     sReplace = "\cf" & lOpenColor & "\" & sClose & "\cf" & lDefaultColor & " "
    62.     sTextRTF = Replace$(sTextRTF, "\" & sClose, sReplace, , , vbTextCompare)
    63.  
    64.     ' Color new line tokens
    65.     If Not mcFormatter.UseRealLineBreaks Then
    66.         ' Use simply sNewLine because backslashes have already
    67.         ' been dealt with when setting sNewLine
    68.         sReplace = "\cf" & lNLColor & sNewLine & "\cf" & lDefaultColor & " "
    69.         sTextRTF = Replace$(sTextRTF, sNewLine, sReplace, , , vbTextCompare)
    70.     End If
    71.  
    72.     ' Color comments last so it overwrites any coloring done inside the comment
    73.     ' - Start comment:
    74.     sReplace = "\cf" & lCommentColor & " " & sComStart
    75.     sTextRTF = Replace$(sTextRTF, sComStart, sReplace, , , vbTextCompare)
    76.     ' - End comment
    77.     sReplace = sComEnd & "\cf" & lDefaultColor & " "
    78.     sTextRTF = Replace$(sTextRTF, sComEnd, sReplace, , , vbTextCompare)
    79.  
    80.     ' Fix all escaped characters
    81.     For i = 0 To UBound(sToEscape)
    82.         sTextRTF = Replace$(sTextRTF, Chr$(i + 1), vbNullChar & sToEscape(i), , , vbTextCompare)
    83.     Next i
    84.  
    85.     ' Undo \\ fix
    86.     sTextRTF = Replace$(sTextRTF, vbNullChar, "\\", , , vbTextCompare)
    87.  
    88.     txtInput.Contents(SF_RTF) = sTextRTF
    89.  
    90.     Call txtInput.SetSelection(lStart, lEnd)
    91.     'txtInput.SelStart = lStart
    92.     Call LockWindowUpdate(0)
    93.  
    94.  
    95. End Sub

    Type some text into the RichTextBox and then have another textbox to hold this:
    VB Code:
    1. TextBox1.Text = RichTextBox1.RTF
    That RTF text is what you will need to edit and learn about if you want this syntax highlighting to be efficient, and even then it is really hard to do when you have a lot of text.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  11. #11
    Lively Member
    Join Date
    Aug 2005
    Posts
    74

    Re: [VS:03] RichText

    I have a calculator program that I have syntax highlighting as an user option. I use the following code. i found the basic and then modified it to fit my needs. It uses arrays of words.
    VB Code:
    1. #Region " Syntax Coloring RichtextBox1 "
    2.     Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    3.     Public Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As Integer) As Integer
    4.  
    5.     Dim FunctionColor As Color = Color.Blue
    6.     Dim UserVariableNameColor As Color = Color.DarkOrange
    7.     Dim OperatorColor As Color = Color.Red
    8.     Dim NumberColor As Color = Color.Black
    9.     Dim ConstantColor As Color = Color.LimeGreen
    10.     Public Enum EditMessages
    11.         LineIndex = 187
    12.         LineFromChar = 201
    13.         GetFirstVisibleLine = 206
    14.         CharFromPos = 215
    15.         PosFromChar = 1062
    16.     End Enum
    17.     Public Sub ColorRtb()
    18.         'Colors the entire RichTextbox
    19.         Dim FirstVisibleChar As Integer
    20.         Dim i As Integer = 0
    21.         While i < RichTextBox1.Lines.Length
    22.             FirstVisibleChar = GetCharFromLineIndex(i)
    23.             ColorLineNumber(i, FirstVisibleChar)
    24.             i += 1
    25.         End While
    26.         If TB.CanFocus Then TB.Focus()
    27.     End Sub
    28.     Public Sub ColorVisibleLines()
    29.         If SyntaxColoring Then
    30.             'Colors the visible lines in the RichTextbox
    31.             Dim FirstLine As Integer = FirstVisibleLine()
    32.             Dim LastLine As Integer = LastVisibleLine()
    33.             Dim FirstVisibleChar As Integer
    34.             Dim i As Integer = FirstLine
    35.             If (FirstLine = 0) And (LastLine = 0) Then
    36.                 Exit Sub
    37.             Else
    38.                 While i < LastLine
    39.                     FirstVisibleChar = GetCharFromLineIndex(FirstLine)
    40.                     ColorLineNumber(FirstLine, FirstVisibleChar)
    41.                     FirstLine += 1
    42.                     i += 1
    43.                 End While
    44.             End If
    45.         End If
    46.     End Sub
    47.     Public Sub ColorLineNumber(ByVal LineIndex As Integer, ByVal lStart As Integer)
    48.         'Colors a single line in the RichTextBox control
    49.         Dim Line As String = RichTextBox1.Lines(LineIndex).ToLower
    50.         Dim i As Integer = 0
    51.         Dim j As Integer = 0
    52.         Dim k As Integer = 0
    53.         Dim SelectionAt As Integer = RichTextBox1.SelectionStart
    54.         Dim str As String
    55.         ' Lock the update
    56.         LockWindowUpdate(RichTextBox1.Handle.ToInt32)
    57.         ' Color the line black to remove any previous coloring
    58.         RichTextBox1.SelectionStart = lStart
    59.         RichTextBox1.SelectionLength = Line.Length
    60.         RichTextBox1.SelectionColor = Color.Black
    61.  
    62.         '' Find any operators
    63.         i = 0
    64.         While i < strOp.Count
    65.             For j = 0 To Line.Length - 1
    66.                 If Line.Substring(j, 1) = strOp(i).ToString Then
    67.                     RichTextBox1.SelectionStart = (lStart + j)
    68.                     RichTextBox1.SelectionLength = 1
    69.                     RichTextBox1.SelectionColor = OperatorColor
    70.                 End If
    71.             Next
    72.             i += 1
    73.         End While
    74.         'check for user defined variable names
    75.         If Names.Count > 0 Then
    76.             i = 0
    77.             While i < Names.Count
    78.                 j = InStr(Line, CStr(Names(i)))
    79.                 If j <> 0 Then
    80.                     RichTextBox1.SelectionStart = (lStart + j - 1)
    81.                     str = CStr(Names(i))
    82.                     RichTextBox1.SelectionLength = str.Length
    83.                     RichTextBox1.SelectionColor = UserVariableNameColor
    84.                 End If
    85.                 i += 1
    86.             End While
    87.         End If
    88.         i = 0
    89.  
    90.         'check for built in function names
    91.         While i < strFunc.Count
    92.             j = InStr(Line, CStr(strFunc(i)))
    93.             If j <> 0 Then
    94.                 RichTextBox1.SelectionStart = (lStart + j - 1)
    95.                 str = CStr(strFunc(i))
    96.                 RichTextBox1.SelectionLength = str.Length
    97.                 RichTextBox1.SelectionColor = FunctionColor
    98.             End If
    99.             i += 1
    100.         End While
    101.         'check for built in constant names
    102.         i = 0
    103.         While i < StrConstant.Count
    104.             str = CStr(StrConstant(i)) 'get the constants name
    105.             j = 0 'reset the pointer
    106.             Do While j < Line.Length - 1 'search for the constant
    107.                 j = Line.IndexOf(str, j) ' get the first instance of the constant
    108.                 Select Case j
    109.                     Case Is = -1 'constant not in line
    110.                         Exit Do
    111.                     Case Is = 0 'start of the string
    112.                         'make sure that the constant is not the start of a word such as e in exp
    113.                         If Line.Substring(j + 1, 1).ToLower Like "[a-z]" Then 'a letter preceeds
    114.                             'not a constant
    115.                         Else
    116.                             RichTextBox1.SelectionStart = (lStart + j)
    117.                             RichTextBox1.SelectionLength = str.Length
    118.                             RichTextBox1.SelectionColor = ConstantColor
    119.                         End If
    120.                     Case Else
    121.                         Select Case (j + str.Length) = Line.Length
    122.                             Case Is = True 'constant is at the end of the line
    123.                                 If Line.Substring(j - 1, 1).ToLower Like "[a-z]" Then 'a letter preceeds
    124.                                     'not a constant
    125.                                 Else
    126.                                     RichTextBox1.SelectionStart = (lStart + j)
    127.                                     RichTextBox1.SelectionLength = str.Length
    128.                                     RichTextBox1.SelectionColor = ConstantColor
    129.                                 End If
    130.  
    131.                             Case Is = False 'the constant is somewhere within the string
    132.                                 If Line.Substring(j - 1, 1).ToLower Like "[a-z]" Then 'a letter preceeds
    133.                                     'not a constant
    134.                                 ElseIf Line.Substring(j + str.Length, 1).ToLower Like "[a-z]" Then 'a letter proceeds
    135.                                     'not a constant
    136.                                 Else
    137.                                     RichTextBox1.SelectionStart = (lStart + j)
    138.                                     RichTextBox1.SelectionLength = str.Length
    139.                                     RichTextBox1.SelectionColor = ConstantColor
    140.                                 End If
    141.                         End Select
    142.                 End Select
    143.                 j += str.Length
    144.             Loop
    145.             i += 1
    146.         End While
    147.         ' Restore the selectionstart
    148.         RichTextBox1.SelectionStart = SelectionAt
    149.         RichTextBox1.SelectionLength = 0
    150.         ' Unlock the update
    151.         LockWindowUpdate(0)
    152.     End Sub
    153.  
    154.     Private Sub RTBTextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    155.         ColorVisibleLines()
    156.         If TB.CanFocus Then TB.Focus()
    157.     End Sub
    158.     Public Function FirstVisibleLine() As Integer
    159.         'Uses API to determine the first line in a RichTextbox control
    160.         Return SendMessage(RichTextBox1.Handle.ToInt32, EditMessages.GetFirstVisibleLine, 0, 0)
    161.     End Function
    162.     Public Function LastVisibleLine() As Integer
    163.         'Returns the number of lines in a RichTextbox
    164.         Dim LastLine As Integer = CInt(FirstVisibleLine() + (RichTextBox1.Height / RichTextBox1.Font.Height))
    165.         If LastLine > RichTextBox1.Lines.Length Or LastLine = 0 Then
    166.             LastLine = RichTextBox1.Lines.Length
    167.         End If
    168.         Return LastLine
    169.     End Function
    170.  
    171.     Public Function GetCharFromLineIndex(ByVal LineIndex As Integer) As Integer
    172.         ' Uses API to get the character
    173.         Return SendMessage(RichTextBox1.Handle.ToInt32, EditMessages.LineIndex, LineIndex, 0)
    174.     End Function
    175.  
    176. #End Region

    You may get something out of it.

  12. #12

    Question Re: [VS:03] RichText

    is not declared error list:
    TB
    SyntaxColoring
    StrOp
    Names
    StrFunc
    StrConstant

    This is for the code posted above.

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