How to connect a listbox to another listbox by their index? can someone help me with this? some codes please. thank you!

can you please elaborate.

Show some examples of what you want to do.

Add some pseudo code.

Regards.

Being new to VB.NET, This may not be the cleanest way to do this, but it works for me.

I have a form that populates a combo box with the dates of employee reports, and then fills another combo box with a list of employee names on the report when the date is selected/changed.

Here is the code I am using to populate the first combo box:

Dim cnnSQL As New System.Data.SqlClient.SqlConnection(My.Settings.ConnectionString)
        Dim strSQL As String = "SELECT row_number()over (order by ReportDate DESC) as ID, ReportDate FROM uvw_ReportDate GROUP BY ReportDate UNION SELECT 0, 'Select Report Date' "
        Dim da As New System.Data.SqlClient.SqlDataAdapter(strSQL, cnnSQL)
        Dim ds As New DataSet
        da.Fill(ds, "uvw_ReportDate")

        'Populates the report selection combobox
        With Me.ComboBox17
            .DataSource = ds.Tables("uvw_ReportDate")
            .DisplayMember = "ReportDate"
            .ValueMember = "ReportDate"
            .SelectedIndex = 0
        End With

When the date is selected, I use the ComboBox17_SelectedIndexChanged to handle the change and fill the employee list with this code:

Dim cnnSQL As New System.Data.SqlClient.SqlConnection(My.Settings.ConnectionString)

            'Populate the Employee Name combo box
            Dim strSQL As String = "SELECT row_number()over (order by EmplyeeName ASC) as ID, EmplyeeName FROM tblManning WHERE CONVERT(varchar,[Rpt Date],101) = '" & Me.ComboBox17.Text & "' GROUP BY EmplyeeName, [Rpt Date] UNION ALL SELECT 0, 'Select Employee' AS EmplyeeName ORDER BY ID ASC"
            Dim da As New System.Data.SqlClient.SqlDataAdapter(strSQL, cnnSQL)
            Dim ds As New DataSet
            da.Fill(ds, "tblManning")

            With Me.ComboBox24
                .DataSource = ds.Tables("tblManning")
                .DisplayMember = "EmplyeeName"
                .ValueMember = "ID"
                .SelectedIndex = 0
            End With

The Union statement adds a "select blah" to the combo box and displays that by default.

Hope this helps.

Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '// create a list of items.
        Dim myList() As String = {"item 1", "item 2", "item 3", "item 4", "item 5", "item 6", "item 7", "item 8", "item 9", "item 10"}
        '// add items to listboxes.
        ListBox1.DataSource = myList
        ListBox2.DataSource = myList
    End Sub

    Private Sub ListBox2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.MouseHover
        '// since index starts at 0 for the first item, -1 would be an index of no item selected.
        If Not ListBox2.SelectedIndex = -1 Then '// if item is selected.
            ListBox1.SelectedIndex = ListBox2.SelectedIndex '// keep same selection.
            '// if Listbox displays scrollbars, the TopIndex will keep both Listboxes displaying the same items.
            ListBox1.TopIndex = ListBox2.TopIndex
        End If
    End Sub

    Private Sub ListBox1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.MouseHover
        If Not ListBox1.SelectedIndex = -1 Then
            ListBox2.SelectedIndex = ListBox1.SelectedIndex
            ListBox2.TopIndex = ListBox1.TopIndex
        End If
    End Sub

End Class

list box code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.