Results 1 to 12 of 12

Thread: Syntax coloring

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Location
    Can't say sorry :)
    Posts
    79

    Syntax coloring

    I wrote this little program for syntax coloring somthing (letters ect).

    But It gives me the error "cannot convert Number to integer" With the first case. Can some one help?

    VB Code:
    1. Private Sub RichTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox.TextChanged
    2.         Dim source As String
    3.  
    4.         Dim rtb As New RichTextBox ' name rtb as a RichText Box type
    5.         Dim Codearray() As String ' It's the array of text
    6.         Dim currentline As String ' The currentline of Text
    7.         Dim Line As Integer ' Dunno why I can't use somthing smaller ...
    8.         Dim I As Decimal 'Largestest thing
    9.         Codearray = Split(source, vbCrLf) ' split Code array from source at Center Left
    10.         For I = LBound(Codearray) To UBound(Codearray) 'From lower case to uppercase
    11.             currentline = Codearray(I) ' Code array for line
    12.             ' add auto tab
    13.             Select Case Left() ' the left case
    14.                 Case "\" ' comments
    15.                     rtb.Text = currentline ' trb on current line
    16.                     rtb.SelectedText = Len(currentline) ' select text
    17.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Italic) ' make her bold!
    18.                     rtb.ForeColor() = Drawing.Color.Gray  'Change color
    19.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    20.  
    21.                 Case "!" ' normal number
    22.                     rtb.Text = currentline ' trb on current line
    23.                     rtb.SelectedText = Len(currentline) ' select text
    24.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Strikeout) ' make her bold!
    25.                     rtb.ForeColor() = Drawing.Color.Purple 'Change color
    26.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    27.                 Case "&" ' Literal
    28.                     rtb.Text = currentline ' trb on current line
    29.                     rtb.SelectedText = Len(currentline) ' select text
    30.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    31.                     rtb.ForeColor() = Drawing.Color.Green  'Change color
    32.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    33.                 Case "%" ' Small number
    34.                     rtb.Text = currentline ' trb on current line
    35.                     rtb.SelectedText = Len(currentline) ' select text
    36.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    37.                     rtb.ForeColor() = Drawing.Color.DarkRed 'Change color
    38.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    39.                 Case ""
    40.                     rtb.Text = currentline ' trb on current line
    41.                     rtb.SelectedText = Len(currentline) ' select text
    42.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Bold) ' make her bold!
    43.                     rtb.ForeColor() = Drawing.Color.AntiqueWhite 'Change color
    44.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    45.                 Case "(" ' comments
    46.                     rtb.Text = currentline ' trb on current line
    47.                     rtb.SelectedText = Len(currentline) ' select text
    48.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Italic) ' make her bold!
    49.                     rtb.ForeColor() = Drawing.Color.Bisque  'Change color
    50.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    51.                 Case ")" ' normal number
    52.                     rtb.Text = currentline ' trb on current line
    53.                     rtb.SelectedText = Len(currentline) ' select text
    54.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Strikeout) ' make her bold!
    55.                     rtb.ForeColor() = Drawing.Color.Bisque 'Change color
    56.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    57.                 Case "{" ' Literal
    58.                     rtb.Text = currentline ' trb on current line
    59.                     rtb.SelectedText = Len(currentline) ' select text
    60.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    61.                     rtb.ForeColor() = Drawing.Color.Brown 'Change color
    62.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    63.                 Case "}" ' Small number
    64.                     rtb.Text = currentline ' trb on current line
    65.                     rtb.SelectedText = Len(currentline) ' select text
    66.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    67.                     rtb.ForeColor() = Drawing.Color.Brown 'Change color
    68.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    69.                 Case ":"
    70.                     rtb.Text = currentline ' trb on current line
    71.                     rtb.SelectedText = Len(currentline) ' select text
    72.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Bold) ' make her bold!
    73.                     rtb.ForeColor() = Drawing.Color.Pink 'Change color
    74.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    75.                     If InStr(1, currentline, "(") > 0 Then
    76.                         rtb.TabIndex = 1
    77.  
    78.                         Dim comment As Long
    79.                         comment = InStr(InStr(1, currentline, ")"), currentline, "*")
    80.  
    81.                         rtb.SelectedRtf = rtb.SelectedRtf & Chr(10) & Chr(13)
    82.                     End If
    83.                 Case Else
    84.                     rtb.Text = currentline ' trb on current line
    85.                     rtb.SelectedText = Len(currentline) ' select text
    86.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Bold) ' make her bold!
    87.                     rtb.BackColor() = Drawing.Color.Red 'Change color
    88.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    89.             End Select
    90.         Next
    91.     End Sub



  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    I don't see how your Select case works at all!

    " Select Case Left() ' the left case "

    What is Left() ??
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Location
    Can't say sorry :)
    Posts
    79
    I didn't compleatly program it. I was transeferring what I could from VB6. And that was in there. I don't even know the function, but last I remember, that wasn't giving me the error.



  4. #4
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    i don't know if this helps but i code like this but only c# comments are colorize. regex function and best for other event not the textchanged. in my case, button click
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.       Dim re As New Regex("(//.*[\n])|(///.*[\n])|(#region.*[\n])|(/\*.*?\*/)|(/\*[^""]*\*/)", RegexOptions.IgnoreCase)
    3.       Dim mc As MatchCollection = re.Matches(RichTextBox1.Text)
    4.       Dim ma As Match
    5.       For Each ma In mc
    6.          RichTextBox1.HideSelection = True
    7.          RichTextBox1.Select(ma.Index, ma.Length)
    8.          RichTextBox1.SelectionColor = Color.Green
    9.          RichTextBox1.SelectionStart = ma.Index + ma.Length
    10.          RichTextBox1.SelectionLength = 0
    11.          RichTextBox1.SelectionColor = Color.Black 'revert the color
    12.       Next
    13.    End Sub
    if this doesn't help, sorry.

    help from pirate and dynamic_sysop

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Word2
    I didn't compleatly program it. I was transeferring what I could from VB6. And that was in there. I don't even know the function, but last I remember, that wasn't giving me the error.
    Hi,

    Before posting code you must check it out yourself to make sure of the problem you are posting.

    Your select case cannot possibly work as posted. Left() has two possibilities in VB.NET. If used in a form it refers to the Left position. If used where a string is the only possibility involved, then it refers to the leftmost characters. Neither of these two circumstances apply to your posted code. It should not even compile, so please check it out yourself and then post your problem.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Location
    Can't say sorry :)
    Posts
    79
    I have checked and replaced. But it now atleast lets me enter letters and such. Althought, it still will not color the string.

    My new code compiles and runs with no runtime errors, but doesn't color the line.

    VB Code:
    1. Private Sub RichTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox.TextChanged
    2.         Dim source As String
    3.  
    4.  
    5.         Dim rtb As New RichTextBox ' name rtb as a RichText Box type
    6.         Dim Codearray() As String ' It's the array of text
    7.         Dim currentline As String ' The currentline of Text
    8.         Dim I As Decimal 'Largestest thing
    9.         Codearray = Split(source, vbCrLf) ' split Code array from source at Center Left
    10.         For I = LBound(Codearray) To UBound(Codearray) 'From lower case to uppercase
    11.             currentline = Codearray(I) ' Code array for line
    12.  
    13.             Select Case currentline
    14.                 Case "\" ' comments
    15.                     rtb.Text = currentline ' trb on current line
    16.                     rtb.SelectedText = Len(currentline) ' select text
    17.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Italic) ' make her bold!
    18.                     rtb.ForeColor() = Drawing.Color.Gray  'Change color
    19.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    20.  
    21.                 Case "!" ' normal number
    22.                     rtb.Text = currentline ' trb on current line
    23.                     rtb.SelectedText = Len(currentline) ' select text
    24.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Strikeout) ' make her bold!
    25.                     rtb.ForeColor() = Drawing.Color.Purple 'Change color
    26.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    27.                 Case "&" ' Literal
    28.                     rtb.Text = currentline ' trb on current line
    29.                     rtb.SelectedText = Len(currentline) ' select text
    30.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    31.                     rtb.ForeColor() = Drawing.Color.Green  'Change color
    32.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    33.                 Case "%" ' Small number
    34.                     rtb.Text = currentline ' trb on current line
    35.                     rtb.SelectedText = Len(currentline) ' select text
    36.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    37.                     rtb.ForeColor() = Drawing.Color.DarkRed 'Change color
    38.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    39.                 Case " "
    40.                     rtb.Text = currentline ' trb on current line
    41.                     rtb.SelectedText = Len(currentline) ' select text
    42.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Bold) ' make her bold!
    43.                     rtb.ForeColor() = Drawing.Color.AntiqueWhite 'Change color
    44.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    45.                 Case "(" ' comments
    46.                     rtb.Text = currentline ' trb on current line
    47.                     rtb.SelectedText = Len(currentline) ' select text
    48.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Italic) ' make her bold!
    49.                     rtb.ForeColor() = Drawing.Color.Bisque  'Change color
    50.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    51.                 Case ")" ' normal number
    52.                     rtb.Text = currentline ' trb on current line
    53.                     rtb.SelectedText = Len(currentline) ' select text
    54.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Strikeout) ' make her bold!
    55.                     rtb.ForeColor() = Drawing.Color.Bisque 'Change color
    56.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    57.                 Case "{" ' Literal
    58.                     rtb.Text = currentline ' trb on current line
    59.                     rtb.SelectedText = Len(currentline) ' select text
    60.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    61.                     rtb.ForeColor() = Drawing.Color.Brown 'Change color
    62.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    63.                 Case "}" ' Small number
    64.                     rtb.Text = currentline ' trb on current line
    65.                     rtb.SelectedText = Len(currentline) ' select text
    66.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    67.                     rtb.ForeColor() = Drawing.Color.Brown 'Change color
    68.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    69.                 Case ":"
    70.                     rtb.Text = currentline ' trb on current line
    71.                     rtb.SelectedText = Len(currentline) ' select text
    72.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Bold) ' make her bold!
    73.                     rtb.ForeColor() = Drawing.Color.Pink 'Change color
    74.                     rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)
    75.                     If InStr(1, currentline, "(") > 0 Then
    76.                         rtb.TabIndex = 2
    77.  
    78.                         Dim comment As Long
    79.                         comment = InStr(InStr(1, currentline, ")"), currentline, "*")
    80.  
    81.                         rtb.SelectedRtf = rtb.SelectedRtf & Chr(10) & Chr(13)
    82.                     End If
    83.                 Case Else
    84.                     rtb.Text = currentline ' trb on current line
    85.                     rtb.SelectedText = Len(currentline) ' select text
    86.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Bold) ' make her bold!
    87.                     rtb.BackColor() = Drawing.Color.Red 'Change color
    88.             End Select
    89.         Next
    90.  
    91.     End Sub



  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Looking at all your posts I still think your Select Case is wrong. Unless there is only one character in currentline I think it should be

    VB Code:
    1. Select Case Microsoft.VisualBasic.Left(currentline, 1)
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Location
    Can't say sorry :)
    Posts
    79
    Unfortunatly that didn't change anything.



  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Word2
    Unfortunatly that didn't change anything.
    Right.

    I have copied your code in full. There are a lot of errors. assuming you have Option Strict On, each of your

    rtb.SelectedText = Len(currentline)

    should be

    rtb.SelectedText = CStr(Len(currentline))

    Also, the string Source never gets filled and I would guess that your code

    rtb.SelectedRtf = rtb.Text = Chr(10) & Chr(13)

    should be

    rtb.SelectedRtf = rtb.Text & Chr(10) & Chr(13)

    ALSO, you cannot declare I as Decimal and then use it as an integer in the array.

    I cannot see how you have made the rtb visible, so have you put one on the form from the toolbox as well as having created an instance in code?




    I do not see how your posted code could possibly have compiled.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  10. #10
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265
    And you need to start learning about debugging procedures. A breakpoint inserted at the beginning of that code (when you do manage to compile it) wil give you a big clue as to where the problem lies - ie is it the Select Case not matching the conditions, or is it the code within the Case startements not doing what you expect.
    Nick.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Location
    Can't say sorry :)
    Posts
    79
    Ok, fixed everything you said, and then some :P.

    I get this error apon running it.

    Invalid file format.

    VB Code:
    1. Private Sub RichTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox.TextChanged
    2.         RichTextBox.Visible = True
    3.  
    4.         Dim source As String
    5.         Dim rtb As New RichTextBox ' name rtb as a RichText Box type
    6.         rtb.Visible = True
    7.         Dim Codearray() As String ' It's the array of text
    8.         Dim currentline As String ' The currentline of Text
    9.         Dim I As Long 'Largestest thing
    10.         Codearray = Split(source, vbCrLf) ' split Code array from source at Center Left
    11.         For I = LBound(Codearray) To UBound(Codearray) 'From lower case to uppercase
    12.             currentline = Codearray(CInt(I)) ' Code array for line
    13.  
    14.             Select Case currentline
    15.                 Case "\" ' comments
    16.                     rtb.Text = currentline ' trb on current line
    17.                     rtb.SelectedRtf = CStr(Len(currentline)) ' select text
    18.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Italic) ' make her bold!
    19.                     rtb.ForeColor() = Drawing.Color.Gray  'Change color
    20.                     rtb.SelectedRtf = rtb.Text & Chr(10) & Chr(13)
    21.  
    22.                 Case "!" ' normal number
    23.                     rtb.Text = currentline ' trb on current line
    24.                     rtb.SelectedRtf = CStr(Len(currentline)) ' select text
    25.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Strikeout) ' make her bold!
    26.                     rtb.ForeColor() = Drawing.Color.Purple 'Change color
    27.                     rtb.SelectedRtf = rtb.Text & Chr(10) & Chr(13)
    28.                 Case "&" ' Literal
    29.                     rtb.Text = currentline ' trb on current line
    30.                     rtb.SelectedRtf = CStr(Len(currentline)) ' select text
    31.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    32.                     rtb.ForeColor() = Drawing.Color.Green  'Change color
    33.                     rtb.SelectedRtf = rtb.Text & Chr(10) & Chr(13)
    34.                 Case "%" ' Small number
    35.                     rtb.Text = currentline ' trb on current line
    36.                     rtb.SelectedRtf = CStr(Len(currentline)) ' select text
    37.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    38.                     rtb.ForeColor() = Drawing.Color.DarkRed 'Change color
    39.                     rtb.SelectedRtf = rtb.Text & Chr(10) & Chr(13)
    40.                 Case ")" ' normal number
    41.                     rtb.Text = currentline ' trb on current line
    42.                     rtb.SelectedRtf = CStr(Len(currentline)) ' select text
    43.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Strikeout) ' make her bold!
    44.                     rtb.ForeColor() = Drawing.Color.Bisque 'Change color
    45.                     rtb.SelectedRtf = rtb.Text & Chr(10) & Chr(13)
    46.                 Case "{" ' Literal
    47.                     rtb.Text = currentline ' trb on current line
    48.                     rtb.SelectedRtf = CStr(Len(currentline)) ' select text
    49.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    50.                     rtb.ForeColor() = Drawing.Color.Brown 'Change color
    51.                     rtb.SelectedRtf = rtb.Text & Chr(10) & Chr(13)
    52.                 Case "}" ' Small number
    53.                     rtb.Text = currentline ' trb on current line
    54.                     rtb.SelectedRtf = CStr(Len(currentline)) ' select text
    55.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Regular) ' make her bold!
    56.                     rtb.ForeColor() = Drawing.Color.Brown 'Change color
    57.                     rtb.SelectedRtf = rtb.Text & Chr(10) & Chr(13)
    58.                 Case ":"
    59.                     rtb.Text = currentline ' trb on current line
    60.                     rtb.SelectedRtf = CStr(Len(currentline)) ' select text
    61.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Bold) ' make her bold!
    62.                     rtb.ForeColor() = Drawing.Color.Pink 'Change color
    63.                     rtb.SelectedRtf = rtb.Text & Chr(10) & Chr(13)
    64.                     If InStr(1, currentline, "(") > 0 Then
    65.                         rtb.TabIndex = 2
    66.  
    67.                         Dim comment As Long
    68.                         comment = InStr(InStr(1, currentline, ")"), currentline, "*")
    69.  
    70.                         rtb.SelectedRtf = rtb.SelectedRtf & Chr(10) & Chr(13)
    71.                     End If
    72.                 Case Else
    73.                     rtb.Text = currentline ' trb on current line
    74.                     rtb.SelectedRtf = CStr(Len(currentline)) ' select text ' ' ' 'Error is here' ' '
    75.                     rtb.SelectionFont = New Font(rtb.SelectionFont, FontStyle.Bold) ' make her bold!
    76.                     rtb.ForeColor() = Drawing.Color.Brown 'Change color
    77.             End Select
    78.         Next I
    79.         CCE.ActiveForm.Text = rtb.Text
    80.         CCE.ActiveForm.Refresh()
    81.  
    82.     End Sub

    I get it in the "rtb.SelectedRtf = CStr(Len(currentline))"
    Line.



  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Location
    Can't say sorry :)
    Posts
    79
    Originally posted by SuperSparks
    And you need to start learning about debugging procedures. A breakpoint inserted at the beginning of that code (when you do manage to compile it) wil give you a big clue as to where the problem lies - ie is it the Select Case not matching the conditions, or is it the code within the Case startements not doing what you expect.
    I changed it. Now it freezes on the Case Else.

    Break points. Will look into.



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