Richtextbox: end of file error..

jaeman

Member
Joined
Jul 15, 2004
Messages
10
Location
Australia
Programming Experience
1-3
Hi,

I'm having a problem when searching through a richtextbox with line & char info running, a line & char variable is the what is causing the problem, returning < 0 ..

When prompted to start from begining it is ok, but if you select no to the start from begining dialog and then select find again the program crashes.

' Find the string in the selected script & select that text
VB.NET:
    Public Function findString(ByVal rtBox As RichTextBox, ByVal searchString As String, ByVal FindOption As RichTextBoxFinds, ByVal start As Integer) As Integer
        ' Check we have a string to search & the rtBox has loaded the file
        If searchString = "" Then
            MsgBox("Nothing to search for!, Please type your search.")
            Exit Function
        ElseIf rtBox.Text = "" Then
            MsgBox("Nothing to search for!, Please load the file first.")
            Exit Function
        End If

        ' Determine the starting location
        Dim index As Integer = rtBox.Find(searchString, start, FindOption)

        ' Determine if the word has been found and select it if it was.
        'If index >= 0 And index < rtBox.TextLength Then
        If index <> -1 Then
            ' Select the string using the index and the length of the string.
            With rtBox
                .Select()
                .Select(index, searchString.Length)
            End With
            C._blnStringFound = True
            Return index + searchString.Length
            Exit Function
        Else
            ' EOF Message - with or without _blnStringFound
            Dim msg As String
            If Not C._blnStringFound Then
                msg = "String not found!"
            Else
                msg = "End of file found!"
            End If
            Dim Result As DialogResult = _
                MessageBox.Show(msg + ", Start from beginning..?", "End of file!", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If Result = DialogResult.Yes Then
                index = 0
                With rtBox
                    .Select()
                    .Select(index, index)
                End With
                findString(rtBox, searchString, FindOption, index)
            Else
                ' Hmmmmmmmmmm
                Exit Function
            End If
        End If
    End Function


' Standard Display Line & Column Information
VB.NET:
    Private Sub displayRTBpositions(ByVal rtBox As RichTextBox)
        Dim intFirstChar As Integer = rtBox.GetCharIndexFromPosition(New Point(0, 0))
        Dim intFirstLine As Integer = rtBox.GetLineFromCharIndex(intFirstChar)

        [COLOR=Red]Dim intCharCount As Integer = (rtBox.SelectionStart + rtBox.SelectionLength) - intFirstChar

[/COLOR]        Dim strFromStart As String = rtBox.Text.Substring(intFirstChar, intCharCount)
        Dim intLine As Integer = strFromStart.Split(vbLf.ToCharArray).Length
        Dim Line As Integer = intFirstLine + intLine

        sbMain.Text = "Ln " + Line.ToString + ", Col " + (CStr(intCharCount - strFromStart.LastIndexOf(vbLf)))
    End Sub

Have tryed a few different methods of counter acting this but seem to get undesirable results. Can anyone enlighten me as to fix this while keeping the line & char indexing still on and leaving the cursor position at its last find point..

Even a simple rtb search example would be great if theres a url around, thx
 
I don't quite get your question, but I see that in first code block your 'start-from-beginning' try to recurse without returning the index (you may not use that index anyway?)
Return findString(rtBox, searchString, FindOption, index)
 
Back
Top