Donacdum
Donacdum

Reputation: 13

List will not remove items, C#

I have a List (named actors) of "Actors", a custom class to be displayed in a ListBox (named listBoxActors) in C#. The user will be able to click an item to highlight and select it, after which they can press a button that Removes that Actor from the list. Here is my code for the button when it is pressed:

Actor current = (Actor) listBoxActors.SelectedItem;

actors.Remove(current);

listBoxActors.DataSource = null;
listBoxActors.DataSource = actors;

However, even after pressing the button, the Actor still displays in the list box as if it had never been removed at all. Setting the DataSource to null and back to the actors list should refresh it (works fine for that purpose when I add actors), but the list remains the same. What should I add/remove? What am I doing wrong?

Upvotes: 0

Views: 61

Answers (1)

Jayakrishnan
Jayakrishnan

Reputation: 4294

You can simply use Remove function:

listBoxActors.Items.Remove(listBoxActors.SelectedItem);

Hope it helps!!

Upvotes: 1

Related Questions