i have a list box and a button. i would like when the button is clicked the selected item in the list box is deleted. any ideas?

Recommended Answers

yes you can do it in this way

Try
            ListBox1.Items.Remove(ListBox1.SelectedItem)
        Catch ex As Exception
            MsgBox(Err.Description)
        End Try

Hope this will Solve your prob .

Regards

Jump to Post

Try this....

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.ListBox1.SelectedIndex > -1 Then
            Me.ListBox1.Items.RemoveAt(Me.ListBox1.SelectedIndex)
        End If
    End Sub
Jump to Post

All 5 Replies

try this on your button click event:

While myListBox.SelectedItems.Count > 0
	myListBox.Items.Remove(myListBox.SelectedItems(0))
End While

yes you can do it in this way

Try
            ListBox1.Items.Remove(ListBox1.SelectedItem)
        Catch ex As Exception
            MsgBox(Err.Description)
        End Try

Hope this will Solve your prob .

Regards

Try this....

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.ListBox1.SelectedIndex > -1 Then
            Me.ListBox1.Items.RemoveAt(Me.ListBox1.SelectedIndex)
        End If
    End Sub

See this following code :

For i As Integer = 0 To ListBox1.SelectedIndices.Count - 1
   ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
Next

Or you use this too :

Do While (ListBox1.SelectedItems.Count > 0)
   ListBox1.Items.Remove(ListBox1.SelectedItem)
Loop
commented: No Need of loop.... -1

thanks for the help :)

Be a part of the DaniWeb community

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