[RESOLVED] [2005]Search a string in record of a ListBox
Hi,
I am using Visual Basic 2005 Express Edition
I have to find a string in record of a ListBox; the string is in fixed place on the record.
I wrote this code and this code working ok:
Code:
For n As Integer = 0 To ListBox1.Items.Count - 1
If Trim(Mid(ListBox1.Items(n), 19, 40)).Trim = TextBox3.Text.Trim Then
MessageBox.Show(Trim(Mid(ListBox1.Items(n), 19, 40)))
End If
Next
I feel that I wrote like old style (VB6)
Can somebody improve this code (To VB2005)?
Re: [2005]Search a string in record of a ListBox
This will show a messagebox if the listbox item contains the search string.
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each item As String In ListBox1.Items
If item.ToLower.Contains(TextBox1.Text.ToLower) Then
MessageBox.Show("Match Found", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Next
End Sub
Re: [2005]Search a string in record of a ListBox
Hi,
I tried your code and it is not working
Even TextBox1.Text is empty your code find a string
Re: [2005]Search a string in record of a ListBox
In that case you need to test for an empty string in the textbox....
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each item As String In ListBox1.Items
If item.ToLower.Contains(TextBox1.Text.ToLower) AndAlso TextBox1.Text <> "" Then MessageBox.Show("Match Found", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Next
End Sub
Re: [2005]Search a string in record of a ListBox
You could make use of the FindString method of the listbox (or FindStringExact) if that's more suitable for your needs:
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Ensure we have a proper string to search for.
If Me.TextBox1.Text <> String.Empty Then
If Me.ListBox1.FindString(Me.TextBox1.Text) Then
MessageBox.Show("Found that string")
End If
End If
End Sub
Re: [2005]Search a string in record of a ListBox
Hi,
To: stimbo
This only finds from beginning of the string.
If somebody wants to see the solution:
http://www.vbcity.com/forums/topic.a...182&#RID465996