Ask Experts Questions for FREE Help !
Ask
    JoshMiles's Avatar
    JoshMiles Posts: 1, Reputation: 1
    New Member
     
    #1

    Nov 13, 2012, 01:00 PM
    Selection.Start returns error when creating a new line..
    Hey,
    I'm really stuck and stumped on how to go about fixing this problem.
    Essentially, the user enters some text into a RichTextBox(Named Content) and whilst the user is typing, it runs a if loop to see if it matches a word, and if it does, it highlights it.
    Here is the code for the Content_TextChanged:
    Code:
     Private Sub Content_TextChanged(sender As Object, e As EventArgs) Handles Content.TextChanged
    
            Dim words() As String = Content.Text.Split(" ")
           
            For Each word As String In words
                If (word.StartsWith("#")) Then
    
                    HighLight(word, Color.LightGreen)
    
                ElseIf (word.StartsWith("<")) Then
    
                    HighLight(word, Color.Aquamarine)
    
                ElseIf (word = "{" Or word = "}" Or word.StartsWith("{") Or word.StartsWith("}") Or word.EndsWith("{") Or word.EndsWith("}")) Then
    
                    
                End If
            Next
    
        End Sub
    As you can see, it asks for the sub HighLight:
    Code:
        Public Sub HighLight(ByVal word As String, ByVal color As Color)
            If (word = "") Then
            Else
    
                Content.SelectionStart = Content.Find(word)
                Content.SelectionColor = color
    
                Content.SelectionStart = Content.TextLength
                Content.SelectionLength = 0
                Content.SelectionColor = color.White
            End If
    
        End Sub
    The error is thrown when you press Enter on the richtextbox after you have just entered something that has had it's colour changed... if you understand me.
    E.G: The user enters "#include" which is changed to the colour Light Green when typing, then the user enters '<stdio.h>' which is changed to Aqua Marine.
    So the user has currently only entered: "#include <stdio.h>"
    The trouble is when the user hits enter to create a new line, it throws this error:
    An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll

    Additional information: InvalidArgument=Value of '-1' is not valid for 'SelectionStart'.
    The problem doesn't occur when the user hits space before going onto a new line.
    Any help?

    Regards,
    Josh
    GaryStaunton's Avatar
    GaryStaunton Posts: 17, Reputation: 2
    New Member
     
    #2

    Nov 14, 2012, 06:01 PM
    Obviously you need to handle the error.

    Content.SelectionStart = Content.Find(word)


    Try assigning the .Find function to a variable and check it first:

    lngWordPos = Content.Find(word)

    If lngWordPos < 0 Then
    '// Do not bother processing, exit the routine
    End If

    Hope that helps
    G

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

When does the IRS start processing amended returns [ 1 Answers ]

When does the IRS start processing amended returns

Script error line 766 [ 1 Answers ]

What is this and how do I fix it? Thanks! Internet explore script error line 766 char 18 access denied code 0 url about:blank This keeps coming up in my son's computer home school curriculum. We've used this curriculum all year with no problems with this script error until now.

IE script error on line 1513 [ 3 Answers ]

I am receiving this script error message (see attachemnt) while processing credit card transactions using the program Booth Tracker, (BT). BT sez the problemis not with their program and when I contacted PayPal they recommended contacting my IE provider which is Microsoft but I am unable to receive...


View more questions Search