Question[Help] Searching A Textbox

Posts 115 of 40 · Page 1 of 3
[Help] Searching A Textbox
How do you search the text in a textbox? (Simpily)
You mean like

Code:
If TextBox1.Text.Contains("lol") Then

        End If
???
Yeah but after that what would you do to highlight it or something
Quote Originally Posted by VernK View Post
Yeah but after that what would you do to highlight it or something
Edit: NVM..
Yeah I think thats it
Yeah, like highligh, and a next, and maybe even find & replace.
There you go:

Code:
    If TextBox1.Text.Contains(UCase(txtSearch.Text)) Then
            TextBox1.SelectionStart = TextBox1.Text.IndexOf(UCase(txtSearch.Text))
            TextBox1.SelectionLength = txtSearch.Text.Length
            TextBox1.Select()
        Else
            Try
                TextBox1.SelectionStart = TextBox1.Text.IndexOf(LCase(txtSearch.Text))
                TextBox1.SelectionLength = txtSearch.Text.Length
                TextBox1.Select()
            Catch ex As Exception
                MsgBox("Not FOUND")
            End Try


        End If
Quote Originally Posted by Lyoto Machida View Post
There you go:

Code:
    If TextBox1.Text.Contains(UCase(txtSearch.Text)) Then
            TextBox1.SelectionStart = TextBox1.Text.IndexOf(UCase(txtSearch.Text))
            TextBox1.SelectionLength = txtSearch.Text.Length
            TextBox1.Select()
        Else
            Try
                TextBox1.SelectionStart = TextBox1.Text.IndexOf(LCase(txtSearch.Text))
                TextBox1.SelectionLength = txtSearch.Text.Length
                TextBox1.Select()
            Catch ex As Exception
                MsgBox("Not FOUND")
            End Try


        End If
Wtfux? If you wanna find something regardless of casing, you make them both uppercase or lowercase (both), what you did just there was check if the textbox contains the uppercase version of the word the person wanted to search for. I wrote a find/replace regex pattern a while back, if you want I can dig it up later.
Quote Originally Posted by Jason View Post


Wtfux? If you wanna find something regardless of casing, you make them both uppercase or lowercase (both), what you did just there was check if the textbox contains the uppercase version of the word the person wanted to search for. I wrote a find/replace regex pattern a while back, if you want I can dig it up later.
OK i want it, But my way works, It search for you text in Upper case if doesnt exist then search in Lower Case, And if both doesnt exists it show a msgbox..
But i dont know how to make like..A button "Next", It just find the first one :O
Quote Originally Posted by Lyoto Machida View Post


OK i want it, But my way works, It search for you text in Upper case if doesnt exist then search in Lower Case, And if both doesnt exists it show a msgbox..
But i dont know how to make like..A button "Next", It just find the first one :O
So basically you're saying your function pretty much just ignores what the user actually wanted to search for (casing) and does whatever it wants. You don't change the casing of the search text at all, so it's pretty much useless for finding all matches.
Quote Originally Posted by Jason View Post


So basically you're saying your function pretty much just ignores what the user actually wanted to search for (casing) and does whatever it wants. You don't change the casing of the search text at all, so it's pretty much useless for finding all matches.
I tried
Quote Originally Posted by Lyoto Machida View Post


I tried
It's what counts ;p

Jason, you have any suggestions?
Quote Originally Posted by Cryptonic View Post


It's what counts ;p

Jason, you have any suggestions?
Multiple ones .

[highlight=vb.net]
Private Function EscapeRegEx(ByVal input As String) As String
Dim replacelist As String = "\[()*?+^$|."
For Each s As String In replacelist
input = input.Replace(s, "\" & s)
Next
Return input
End Function

Private Sub FindAndReplace(ByVal textCtrl As Control, ByVal needle As String, ByVal replacement As String)
Dim haystack As String = textCtrl.Text
textCtrl.Text = Regex.Replace(haystack, "\b" & EscapeRegEx(needle) & "\b", replacement, RegexOptions.IgnoreCase Or RegexOptions.Compiled)
End Sub
[/highlight]

To use:

[highlight=vb.net]
FindAndReplace(RichTextBox1, "jason is cool", "jason is very cool")
[/highlight]

God it is weird coding in VB again.
Quote Originally Posted by Jason View Post


Multiple ones .

[highlight=vb.net]
Private Function EscapeRegEx(ByVal input As String) As String
Dim replacelist As String = "\[()*?+^$|."
For Each s As String In replacelist
input = input.Replace(s, "\" & s)
Next
Return input
End Function

Private Sub FindAndReplace(ByVal textCtrl As Control, ByVal needle As String, ByVal replacement As String)
Dim haystack As String = textCtrl.Text
textCtrl.Text = Regex.Replace(haystack, "\b" & EscapeRegEx(needle) & "\b", replacement, RegexOptions.IgnoreCase Or RegexOptions.Compiled)
End Sub
[/highlight]

To use:

[highlight=vb.net]
FindAndReplace(RichTextBox1, "jason is cool", "jason is very cool")
[/highlight]

God it is weird coding in VB again.
All I see is Find & Replace, I was manily focused on Searching and find next.
Quote Originally Posted by Cryptonic View Post


All I see is Find & Replace, I was manily focused on Searching and find next.
Well you did say you also wanted find and replace. Here's a find all. find and find next are a bit more work (not much) but it's 4:35am and I'm really not in the mood.

[highlight=vb.net]
Public Sub FindAll(ByVal inputBox As RichTextBox, ByVal highlightColor As Color, ByVal needle as string)
For each m As Match In Regex.Matches(inputBox.Text, "\b" & EscapeRegEx(needle) & "\b", RegexOptions.IgnoreCase Or RegexOptions.Compiled)
inputBo*****lectionStart = m.Index
inputBo*****lectionLength = m.Length
inputBo*****lectionBackcolor = highlightColor
inputBox.DeselectAll()
Next
inputBo*****lectionBackColor = inputBox.BackColor
End Sub
[/highlight]
Posts 115 of 40 · Page 1 of 3
This thread is closed for replies.

Tags for this Thread

None

Need help?