Question search a textbox (not richtextbox) it works, i need to implement 'find next' feature.

Ishraq

Member
Joined
Feb 2, 2011
Messages
5
Programming Experience
Beginner
i use the below code to search and highlight the matching text, but it only shows the first instance of match (only 1 result), i would like the next matching text to get highlighted each time i click the button (like a 'find next' button)
i also need to get the line number of highlighted item. thank you. :)

'textbox1 is the textbox with content i need searched.
'textbox2 is where search string is typed.


Public Overloads Function SearchText(ByVal textToFind As String, Optional ByVal startPosition As Integer = 0, Optional ByVal endPosition As Integer = 0, Optional ByVal highlightText As Boolean = True, Optional ByVal matchCase As Boolean = False) As Integer

Dim i As Integer

If endPosition < 1 Then
If Not matchCase Then
textToFind = textToFind.ToLower
Dim temp As String = TextBox1.Text.ToLower
i = temp.IndexOf(textToFind, startPosition, Me.Text.Length)
Else
i = TextBox1.Text.IndexOf(textToFind, startPosition, Me.Text.Length)
End If
Else
If matchCase = False Then
textToFind = textToFind.ToLower
Dim temp As String = TextBox1.Text.ToLower
i = temp.IndexOf(textToFind, startPosition, endPosition)
Else
i = TextBox1.Text.IndexOf(textToFind, startPosition, endPosition)
End If
End If
If i > -1 Then
If highlightText Then
TextBox1.Focus()
TextBox1.SelectionStart = i
TextBox1.SelectionLength = textToFind.Length
End If
End If

Return i
End Function



' i would like the below button to be a "find next" button, also to show the line number in msgbox of highlighted item, like msgbox(linenumber)

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

SearchText(TextBox2.Text, 0, TextBox1.TextLength, True, False)

End Sub
 
ok i found out how to get line number, just need 'find next'

but if u know a better way to do this then pls tell... also will this work on server side, if i use it on a web page?

Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Public Const EM_LINEFROMCHAR = &HC9

Dim lineNum As Int32


Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

'Call the API to get the Line # from the current char.
lineNum = SendMessage(TextBox1.Handle.ToInt32, EM_LINEFROMCHAR, -1, 0)

'Windows sees the first line in a edit box as line 0. So just add a value of 1 to the result if you want the first line to be line #1.
TextBox2.Text = "Current Line: #" & (lineNum + 1).ToString

'Check for line count first to keep a possible error from happening if the textbox has none.
If TextBox1.Lines.Length > 0 Then
'Get the text at the current line position.
TextBox3.Text = TextBox1.Lines.GetValue(lineNum)

End If
End Sub
 
please forgive my stupidity.. heh this works in getting line number too..

Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
TextBox2.Text = TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart)
End Sub
 
help Any1???
there is 1 way this can be done, that is to be able to change the start position, to the previous match's end position.. i tried to do this but an error comes, it doesn't allow me to change the start position from 0 to anything else.. pls help sum1!!!
this is how i did..
this is what i did, but didn't work..



dim z as integer = 0



Public Overloads Function SearchText(ByVal textToFind As String, Optional ByVal startPosition As Integer = 0, Optional ByVal endPosition As Integer = 0, Optional ByVal highlightText As Boolean = True, Optional ByVal matchCase As Boolean = False) As Integer



Dim i As Integer

If endPosition < 1 Then

If Not matchCase Then

textToFind = textToFind.ToLower

Dim temp As String = TextBox1.Text.ToLower

i = temp.IndexOf(textToFind, startPosition, Me.Text.Length)

Else

i = TextBox1.Text.IndexOf(textToFind, startPosition, Me.Text.Length)

End If

Else

If matchCase = False Then

textToFind = textToFind.ToLower

Dim temp As String = TextBox1.Text.ToLower

i = temp.IndexOf(textToFind, startPosition, endPosition)

Else

i = TextBox1.Text.IndexOf(textToFind, startPosition, endPosition)

End If

End If

If i > -1 Then

If highlightText Then

TextBox1.Focus()

TextBox1.SelectionStart = i

' 'i' is the starting position of first match, so i + texttofind.length would give the next start position right???

z = i + textToFind.Length

TextBox1.SelectionLength = textToFind.Length

End If

End If

Return i

End Function



Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

SearchText(TextBox2.Text, z, TextBox1.TextLength, True, False)

End Sub

This is the error i got :

Count must be positive and count must refer to a location within the string/array/collection.

Parameter name: count



Please help! what am i doing wrong?? :s
 
ok this forum has been so much help! :S haha.. i figured it out.. heres how i did it (using another method though) n this 1 selects the whole line coz thats what i need.. it can be changed easily to select only search word..

:) i kno other ppl can do it alot less messy but hey as long as it works right! heh ok heres how i did it...

dim z as integer

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

z = 0

End Sub


Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

On Error Resume Next

Dim x As Integer = 0

If z = 0 Then

If TextBox1.Lines(z).ToString().Contains(TextBox2.Text) Then

TextBox1.Focus()

TextBox1.Select(x, TextBox1.Lines(z).LongCount) 'change here to select word only

TextBox1.ScrollToCaret()

z = z + 1

x = 0

Exit Sub



End If

z = z + 1

End If

1: If z > 0 And z <> TextBox1.Lines.Count Then



If TextBox1.Lines(z).ToString().Contains(TextBox2.Text) Then

x = 0

For i = 0 To (z - 1)

If TextBox1.Lines(i).LongCount < 2 Then

x = x + 2

Else



x = x + TextBox1.Lines(i).LongCount + 2

End If



Next

TextBox1.Focus()

TextBox1.Select(x, TextBox1.Lines(z).LongCount) 'change here to select word only

TextBox1.ScrollToCaret()

z = z + 1

x = 0

Exit Sub

End If

z = z + 1

GoTo 1

End If





If z = TextBox1.Lines.Count Then



If TextBox1.Lines(z).ToString().Contains(TextBox2.Text) Then

For i = 0 To (z - 1)

If TextBox1.Lines(i).LongCount < 2 Then

x = x + 2

Else



x = x + TextBox1.Lines(i).LongCount + 2

End If



Next

TextBox1.Focus()

TextBox1.Select(x, TextBox1.Lines(z).LongCount) 'change here to select word only

TextBox1.ScrollToCaret()

z = 0

x = 0

Exit Sub

End If

z = 0



End If

End Sub
 
Last edited:
Back
Top