Guest pyro81 Posted December 5, 2002 Posted December 5, 2002 (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 December 5, 2002 by divil Quote
*Gurus* divil Posted December 5, 2002 *Gurus* Posted December 5, 2002 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Guest pyro81 Posted December 5, 2002 Posted December 5, 2002 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) Quote
wyrd Posted December 5, 2002 Posted December 5, 2002 indexof does not perform case sensitive searches :eek: Yes it does. Quote Gamer extraordinaire. Programmer wannabe.
Guest pyro81 Posted December 5, 2002 Posted December 5, 2002 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. Quote
*Experts* Volte Posted December 6, 2002 *Experts* Posted December 6, 2002 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] Quote
wyrd Posted December 6, 2002 Posted December 6, 2002 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)) Quote Gamer extraordinaire. Programmer wannabe.
Guest pyro81 Posted December 6, 2002 Posted December 6, 2002 (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 December 6, 2002 by divil Quote
*Gurus* divil Posted December 6, 2002 *Gurus* Posted December 6, 2002 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Guest pyro81 Posted December 6, 2002 Posted December 6, 2002 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? Quote
*Gurus* divil Posted December 6, 2002 *Gurus* Posted December 6, 2002 Post all the code you have here, including where you save the last position found, and I'll take a look. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Guest pyro81 Posted December 7, 2002 Posted December 7, 2002 (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 December 7, 2002 by divil Quote
*Gurus* divil Posted December 7, 2002 *Gurus* Posted December 7, 2002 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 Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Guest pyro81 Posted December 7, 2002 Posted December 7, 2002 thanks Divil that works a treat.....thanks for all the help that has been bugging me for ages... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.