In this post,We’ ll provide to delete the selected item in listbox, when you click the Delete Button.
We can do this in 2 different ways.
Remove Method
When you remove an item from the list, the indexes change for subsequent items in the list. All information about the removed item is deleted. You can use this method to remove a specific item from the list by specifying the actual item to remove from the list.
You can remove selected items from listbox when pressed delete button by using the following code snippet.
1 2 3 4 5 6 | private void button1_Click(object sender, EventArgs e) { listBox1.Items.Remove(listBox1.SelectedItem); } |
RemoveAt Method
When you remove an item from the list, the indexes change for subsequent items in the list. All information about the removed item is deleted. You can use this method to remove a specific item from the list by specifying the index of the item to remove from the list.
You can remove selected items from listbox when pressed delete button by using the following code snippet.
1 2 3 4 5 6 | private void button2_Click(object sender, EventArgs e) { listBox1.Items.RemoveAt(listBox1.SelectedIndex); } |
Support