How to remove a selected item in a listbox by double-click

How can I remove a selected item in a list box by double-clicking?

If this requires a procedure please provide an example.

Thanks for your time
LVL 1
Stella PauleyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
The extra code in the MouseDown event will prevent the selected index from being deleted if you double click in the ListBox below the last item.  You also need to check if there is no selected item before attempting to delete it:

    Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
        ListBox1.SelectedIndex = ListBox1.IndexFromPoint(New Point(e.X, e.Y))
    End Sub

    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
        If ListBox1.SelectedIndex <> -1 Then
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        End If
    End Sub
0
 
Fahad MukhtarDistinguished EngineerCommented:
put this line in the Doubleclick Event of the Listbox:

ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
0
All Courses

From novice to tech pro — start learning today.