Results 1 to 2 of 2

Thread: Searching a Listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    4

    Question Searching a Listbox

    How do you search a listbox with items already inside using string and indexof function? Also, could I display the results in another listbox? What do you guys suggest. Thanks

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477

    Re: Searching a Listbox

    Originally posted by anteneh
    How do you search a listbox with items already inside using string and indexof function? Also, could I display the results in another listbox? What do you guys suggest. Thanks
    Like this:

    VB Code:
    1. Imports System
    2. Imports System.Data
    3. Imports System.Data.SqlClient
    4.  
    5. Public Class Form1
    6.     Inherits System.Windows.Forms.Form
    7.  
    8.     Private Sub btnGetData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetData.Click
    9.         Dim cnCustomers As New SqlConnection("packet size=4096;integrated security=SSPI;data source=(local);" & _
    10.                                              "persist security info=False;initial catalog=northwind")
    11.         Dim daCustomers As New SqlDataAdapter("SELECT CustomerID, CompanyName, ContactName, Address FROM Customers", cnCustomers)
    12.         Dim dtCustomers As New DataTable("Customers")
    13.         Dim drCustomer As DataRow
    14.         daCustomers.Fill(dtCustomers)
    15.         For Each drCustomer In dtCustomers.Rows
    16.             Me.ListBox1.Items.Add(Convert.ToString(drCustomer("ContactName")))
    17.         Next
    18.     End Sub
    19.  
    20.     Private Sub btnFindItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindItem.Click
    21.         Dim liCust As Object
    22.         For Each liCust In Me.ListBox1.Items
    23.             If Convert.ToString(liCust).IndexOf("Ana") > -1 Then Me.ListBox2.Items.Add(liCust)
    24.         Next
    25.     End Sub
    26.  
    27.     Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
    28.         Me.Close()
    29.     End Sub
    30. End Class
    Whadayamean it doesn't work....
    It works fine on my machine!

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