I have a listbox which contains items in it. After you click on the selected item, the listbox will be populated with different items relevant to the item which you clicked on.
What I want to make, is instead of having to click on the selected item, I want to press the Enter key when an item is selected, and that way I won't need to click on the items.
This is what I tried for a KeyDown event:

Private Sub ListBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ListBox1.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            ListBox1_SelectedIndexChanged(sender, e)
        End If
End Sub

Recommended Answers

All 3 Replies

Wait, where do you select the item that is populated to a ListBox? Your details is not providing enough information, I don't understand of your components. Please clarify that and I can help you with your problem.

OK, here is the code which populates the listbox with new items if one of the current items is clicked:

 'Changes the items in the listbox if item is clicked
        Dim row As Object
        row = DirectCast(ListBox1.SelectedItem, System.Data.DataRowView).Row

        If (TypeOf row Is CumulativeProject.MainDatabaseDataSet.DepartmentsRow) Then
            Dim dept As String

            dept = DirectCast(row, CumulativeProject.MainDatabaseDataSet.DepartmentsRow).Department
            dept = Trim(dept)

            ListBox1.DisplayMember = ""
            ListBox1.ValueMember = ""
            ListBox1.DataSource = TeachersInDepartmentTableAdapter.GetData(dept)
            'ListBox1.DataSource = NamesTableAdapter.GetData(dept)
            ListBox1.DisplayMember = "Last_First_Name"
            ListBox1.ValueMember = "id"
        End If

Can you give a picture not the image but the clue or make an example of the data you are talking about because now with your new post I can see that you also have a database where you want to retrieve the info that you are talking about. What kind or type of info you have in the database and what's their relationship, mean as you want to only populate the related to the selection items, make an example that will give us a clue because we don't know exactly how data is related to one another in your database.

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.