CodeItBetter Programming Another VB Programming Blog

How to Search for a word in a Text box.

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'Controls - How to Search for a word in a Text box.
'Insert a Text box and a command button on a form and insert the following code:

'In a module
Public 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
 
Public 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
 
'In a form
Private Sub cmdFindNext_Click()
    If Text1.SelStart = 0 Then
        Text1.SelStart = 1
    End If
    Call FindNext(Text1, "Test", False)
End Sub
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.