Jump to content
Xtreme .Net Talk

searching for text in textbox..HELP


Recommended Posts

Posted (edited)

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

 

   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:

Edited by divil
  • *Gurus*
Posted

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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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)

Posted
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.
  • *Experts*
Posted

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]

Posted

Ah, I misread. You can get around this easily though..

 

Dim wordToFind As String = "SOmEthiNg"
Dim stringToFindIn As String = "a string with something in it"
Console.WriteLine(stringToFindIn.ToLower.IndexOf(wordToFind.ToLower))

Gamer extraordinaire. Programmer wannabe.
Posted (edited)

ok guys i am using the following code now to search

 

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.???

Edited by divil
Posted

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?

Posted (edited)

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.

 

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!

Edited by divil
  • *Gurus*
Posted

You haven't made your variable to save the last position found. This'll need to be declared outside the scope of that method.

 

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

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

thanks Divil that works a treat.....thanks for all the help

that has been bugging me for ages...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...