Results 1 to 9 of 9

Thread: Searching a Textbox for a word???

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Leeds, UK
    Posts
    287

    Post

    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

  2. #2
    Junior Member
    Join Date
    Oct 1999
    Location
    Colorado Springs
    Posts
    17

    Post

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Leeds, UK
    Posts
    287

    Post

    Yeah!

    It helped a lot! Thanks.

  4. #4
    Junior Member
    Join Date
    Oct 1999
    Location
    Colorado Springs
    Posts
    17

    Post

    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.

  5. #5
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Post

    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



  6. #6
    Junior Member
    Join Date
    Oct 1999
    Location
    Colorado Springs
    Posts
    17

    Post

    Thanks, Yonatan. Hey, is there an easy way to use regular expressions within a search, or am I speaking the wrong language here?

  7. #7
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Post

    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



  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Post

    Please describe what you mean by "regular expression".

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

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

  9. #9
    Junior Member
    Join Date
    Oct 1999
    Location
    Colorado Springs
    Posts
    17

    Post

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width