PDA

Click to See Complete Forum and Search --> : Searching a Textbox for a word???


rino_2
Nov 5th, 1999, 01:21 AM
Hi,

I would like to click a button that will search my text box called txtWord and look for the word 'pease'. I know this is possible using the inStr(object) etc... But can somebody please tell me that exact code???

Thanks

blofeld3
Nov 5th, 1999, 01:25 AM
I used this in one of my programs. It gives the user an input box and finds (and selects the searchword)

Private Sub mnufind_Click()
Dim Search, Where ' Declare variables.

' Get search string from user.
Search = InputBox("Enter text to be found:")
Where = InStr(txtinfo.Text, Search) ' Find string in text.
If Where Then ' If found,
txtinfo.SelStart = Where - 1 ' set selection start and
txtinfo.SelLength = Len(Search) ' set selection length.
Else
MsgBox "Text not found." ' Notify user.
End If

End Sub

I hope this helps

rino_2
Nov 5th, 1999, 01:59 AM
Yeah!

It helped a lot! Thanks.

blofeld3
Nov 5th, 1999, 02:08 AM
You bet...Keep in mind, the code only finds the first occurrence of the search word. I don't have any code for a "find next" feature handy, but I'm sure someone else does. Come to think of it, I could use it myself.

Yonatan
Nov 5th, 1999, 02:20 AM
Try this:

Private Sub FindFirst(txtBox As TextBox, ByVal sWord As String, Optional ByVal bCaseSensitive As Boolean = False)
txtBox.SelStart = 1
Call FindNext(txtBox, sWord, bCaseSensitive)
End Sub

Private Sub FindNext(txtBox As TextBox, ByVal sWord As String, Optional ByVal bCaseSensitive As Boolean = False)
Dim iPos As Integer
iPos = InStr(txtBox.SelStart + txtBox.SelLength, txtBox.Text, sWord, IIf(bCaseSensitive, vbBinaryCompare, vbTextCompare))
If iPos > 0 Then
txtBox.SelStart = iPos - 1
txtBox.SelLength = Len(sWord)
Else
txtBox.SelStart = 0
End If
End Sub

------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)

blofeld3
Nov 5th, 1999, 02:40 AM
Thanks, Yonatan. Hey, is there an easy way to use regular expressions within a search, or am I speaking the wrong language here?

Yonatan
Nov 5th, 1999, 02:43 AM
You're speaking the right language. It's just not the language I'm speaking...

------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)

MartinLiss
Nov 5th, 1999, 03:08 AM
Please describe what you mean by "regular expression".

------------------
Marty

[This message has been edited by MartinLiss (edited 11-05-1999).]

blofeld3
Nov 5th, 1999, 03:21 AM
I was thinking of something like keying in

/^-\d/

to search for a line starting with a negative number. Is this supported in VB? I haven't seen any documentation, so I doubt it is.