jeudi 28 août 2014

c# - Delete an List item by listbox


Vote count:

0




I have here a class and a List for my Purchasers:



public class Purchaser
{
public string Name { get; set; }
public bool Paid { get; set; }
}

List<Purchaser> Purchasers = new List<Purchaser>();


I also have a code to add new purchasers by cathing the text from one textbox and adding it to the Purchaser List and to my listbox listDOF:



if(box_AddPerson.TextLength != 0)
{
Purchaser purchaser = new Purchaser();
purchaser.Nome = caixa_AddJog.Text;
purchaser.Paid = true;
listDOF.Items.Add(purchaser.Name);
Purchasers.Add(purchaser);
}


This code works well, the purchaser is added to the Purchaser List and also to my listbox.

Now what I need is be able to remove one purchaser by the listbox. I tried this, in the click event of one button:



if(listDOF.SelectedItems.Count != 0)
{
Purchaser purchaser = new Purchaser();
Purchasers.Remove(purchaser);
listDOF.Items.Remove(listDOF.SelectedItem);
}


This works well for removing the selected purchaser from the listbox. But how to also remove him from the Purchaser List?

Thanks all in advance!



asked 17 secs ago







c# - Delete an List item by listbox

Aucun commentaire:

Enregistrer un commentaire