I have a ListBox with elements and I need to display the elements, I have that working but I want to add an option to remove the selected item, here is the problem... I removed it but I was removing from the listbox not from the elements class, what can I do to remove the items from the tree? Here is my code:
Form 1:
private void btnBuildPartsList_Click(object sender, EventArgs e)
{
Assembly topWidget = new Assembly("Main Assembly", 1);
Assembly chassis = new Assembly("Chassis", 1);
Assembly display = new Assembly("Display", 1);
Assembly powerSupply = new Assembly("Power Supply", 1);
Part bolt = new Part("Bolt", 24);
Part mainCase = new Part("Case", 1);
Part screen = new Part("Screen", 3);
Part displayElectronics = new Part("Display Electronics", 3);
Part transformer = new Part("Transformer", 1);
Part powerBoard = new Part("Power Board", 1);
Part ductTape = new Part("Duct Tape", 5);
Part gum = new Part("Bubble Gum", 25);
topWidget.Add(chassis);
topWidget.Add(display);
topWidget.Add(powerSupply);
chassis.Add(mainCase);
chassis.Add(bolt);
display.Add(screen);
display.Add(displayElectronics);
powerSupply.Add(transformer);
powerSupply.Add(powerBoard);
topWidget.Add(ductTape);
topWidget.Add(gum);
DisplayPartsList(topWidget);
}
private void DisplayPartsList(Assembly topWidget)
{
List<string> partsList = new List<string>();
topWidget.WritePartsList(partsList);
listParts.Items.Clear();
listParts.Items.AddRange(partsList.ToArray());
}
And here is my Assembly class:
namespace Composite
{
public class Assembly : BaseAssembly
{
private List<BaseAssembly> elements = new List<BaseAssembly>();
public Assembly(string name, int quantity)
: base(name, quantity)
{
}
public override void Add(BaseAssembly assembly)
{
elements.Add(assembly);
}
public override void Remove(BaseAssembly assembly)
{
elements.Remove(assembly);
}
public override void WritePartsList(List<string> list)
{
list.Add(Quantity.ToString() + " - " + Name);
foreach (BaseAssembly assembly in elements)
{
assembly.WritePartsList(list);
}
}
}
}
I think that this code is enough, all the other code it's like in other Composite Desing pattern code, now, how can I do that remove item function? Selecting x
element from my listbox. Like I said before I was removing the item from the listbox
but I want to remove it from all the class. Thanks in advance.