searching for text in textbox..HELP

  • Thread starter Thread starter pyro81
  • Start date Start date
P

pyro81

Guest
i am using the following code to search for strings in a textbox.

Visual Basic:
    Function findstring()
        Dim sstart As Integer
        Dim srchmode As CompareMethod
        Me.TopMost = True
        If chkbox.Checked = True Then
            srchmode = CompareMethod.Binary
        Else
            srchmode = CompareMethod.Text
        End If
        sstart = InStr(textbox1.SelectedText + 2, textbox1.Text, findword.Text, srchmode)
        If sstart = 0 Then
            MsgBox("No Match Found!",MsgBoxStyle.Information)         
        Else
            textbox1.Select(sstart - 1, findword.Text.Length)
            textbox1.ScrollToCaret()
        End If
    End Function

this searches for the string in the textbox from where the cursor is downwards....that is find but how can i get it to search from were the cursor is upwards...i know you can use INSTRREV but i cannot get anything to work...please help!:confused:
 
Last edited by a moderator:
Instrrev should work, although it's probably better to use the IndexOf and LastIndexOf methods of the String class. They're likely to have better documentation and samples for.

If you still can't get it to work, post the code you're using to search backwards and I'll have another look.
 
i am only using instr as indexof does not perform case sensitive
searches

the code i am using to perform reverse search is the same as above but with

sstart = instrrev(textbox1.selectedtext_
-1,textbox1.text,findword.text,srchmode)
 
if i have the word CPU in upper case in a textbox and i type cpu in lower case into the find dialog box the indexof function does not find it, instr will.
 
Keep in mind that IndexOf will return 0 if it is in the first position,
and -1 if it is not found. The positions are 0-based in .NET.
I believe InStr returns 0 if it's not found. Try typing 'blahblahCPU'
and trying it.

[edit]Huh. You seem to be correct. Try adding Option Compare Text above
the form class.[/edit]
 
Ah, I misread. You can get around this easily though..

Visual Basic:
Dim wordToFind As String = "SOmEthiNg"
Dim stringToFindIn As String = "a string with something in it"
Console.WriteLine(stringToFindIn.ToLower.IndexOf(wordToFind.ToLower))
 
ok guys i am using the following code now to search

Visual Basic:
 Dim sstart As Integer
        If chkbox.Checked = False Then
            sstart = textbox1.Text.ToLower.IndexOf_(findword.Text.ToLower)
        If sstart < 0 Then
            MsgBox("No Match Found")        
    Exit Sub
        End If
        textbox1.Select(sstart, findword.Text.Length)
        textbox1.ScrollToCaret()


this works fine and finds the first instance of the string entered into the 'findword' textbox. How can i modify this code so that when you hit find again it finds the next instance of the same string.???
 
Last edited by a moderator:
One of the IndexOf overloads accepts a Start parameter. You'll have to save in a variable the last result found, and next time they click it, specify this (+1) as the start position to find.
 
thanks for the post Divil

i have been using the +1 you wrote about in your postand it works grat but i cannoy get i t to work for the code above.......
can you help?
 
Post all the code you have here, including where you save the last position found, and I'll take a look.
 
Thanks Divil i have posted the code below. the 'Texted.txtbox' is the name of the text box that the txt file is loaded into and the 'Findword.text' is the name of the text box where the search word is entered.

Visual Basic:
Sub findstring()
        Me.TopMost = True
        Dim srchstart As Integer
        If chkbox.Checked = False Then
            srchstart = texted.txtbox.Text.ToLower.IndexOf(findword.Text.ToLower, +1)
        Else
            srchstart = texted.txtbox.Text.IndexOf(findword.Text, +1)
        End If
        If srchstart < 0 Then
            MsgBox("No Match Found", MsgBoxStyle.Information, "TextEd Find")
            Exit Sub
        End If
        texted.txtbox.Select(srchstart, findword.Text.Length)
        texted.txtbox.ScrollToCaret()
    End Sub

what i want is when i press the find button it locates the 1st instance of that string. then when i press it again it finds the next instance and so on untill it reached the end of the file.

thanks for your continued help!
 
Last edited by a moderator:
You haven't made your variable to save the last position found. This'll need to be declared outside the scope of that method.

Visual Basic:
Private intLastPos As Integer = 0

Sub findstring()
        Me.TopMost = True
        Dim srchstart As Integer
        If chkbox.Checked = False Then
            srchstart = texted.txtbox.Text.ToLower.IndexOf(findword.Text.ToLower, intLastPos +1)
        Else
            srchstart = texted.txtbox.Text.IndexOf(findword.Text, intLastPos +1)
        End If
        If srchstart < 0 Then
            MsgBox("No Match Found", MsgBoxStyle.Information, "TextEd Find")
            Exit Sub
        End If
        texted.txtbox.Select(srchstart, findword.Text.Length)
        texted.txtbox.ScrollToCaret()

  intLastPos = srchstart
End Sub

You'll probably want another sub to reset it back to the beginning I suppose, by doing intLastPos = 0
 
thanks Divil that works a treat.....thanks for all the help
that has been bugging me for ages...
 
Back
Top