Results 1 to 6 of 6

Thread: [RESOLVED] [2005]Search a string in record of a ListBox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Resolved [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)?

  2. #2
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         For Each item As String In ListBox1.Items
    3.             If item.ToLower.Contains(TextBox1.Text.ToLower) Then
    4.                 MessageBox.Show("Match Found", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    5.             End If
    6.         Next
    7.     End Sub
    If my post helps , please feel free to rate it

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    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

  4. #4
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    
    2.         For Each item As String In ListBox1.Items
    3.             If item.ToLower.Contains(TextBox1.Text.ToLower) AndAlso TextBox1.Text <> "" Then                MessageBox.Show("Match Found", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    4.             End If
    5.         Next
    6.     End Sub
    If my post helps , please feel free to rate it

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.         ' Ensure we have a proper string to search for.
    4.         If Me.TextBox1.Text <> String.Empty Then
    5.  
    6.             If Me.ListBox1.FindString(Me.TextBox1.Text) Then
    7.                 MessageBox.Show("Found that string")
    8.             End If
    9.  
    10.         End If
    11.  
    12.     End Sub
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    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
  •  



Click Here to Expand Forum to Full Width