|
-
Mar 31st, 2007, 11:29 AM
#1
Thread Starter
Addicted Member
[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)?
-
Mar 31st, 2007, 11:45 AM
#2
Hyperactive Member
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
If my post helps , please feel free to rate it 
-
Mar 31st, 2007, 12:05 PM
#3
Thread Starter
Addicted Member
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
-
Mar 31st, 2007, 03:10 PM
#4
Hyperactive Member
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
If my post helps , please feel free to rate it 
-
Mar 31st, 2007, 06:58 PM
#5
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
-
Apr 1st, 2007, 12:44 AM
#6
Thread Starter
Addicted Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|