复合设计模式

时间:2016-11-18 03:17:15

标签: c# winforms

我有一个带有元素的ListBox,我需要显示元素,我有这个工作,但我想添加一个选项来删除所选项目,这是问题...我删除它但我正在删除listbox不是来自元素类,我该怎么做才能从树中删除项目?这是我的代码:

表格1:

index_arr

这是我的大会班:

namespace EraseJunkFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo yourRootDir = new DirectoryInfo(@"C:\somedirectory\");
            foreach (DirectoryInfo dir in yourRootDir.GetDirectories())
                    DeleteDirectory(dir.FullName, true);
        }
        public static void DeleteDirectory(string directoryName, bool checkDirectiryExist)
        {
            if (Directory.Exists(directoryName))
                Directory.Delete(directoryName, true);
            else if (checkDirectiryExist)
                throw new SystemException("Directory you want to delete is not exist");
        }
    }
}

我认为这段代码已经足够了,所有其他代码就像其他Composite Desing模式代码一样,现在,我该怎样做才能删除项目功能?从我的列表框中选择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()); } 元素。就像我之前说的那样,我从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); } } } } 删除了该项目,但是我想将它从所有类中删除。提前致谢。

1 个答案:

答案 0 :(得分:1)

我有一个解决方案,首先将此代码作为全局变量:

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);

之后你需要处理delete函数如何做到这一点?使用按钮或仅从列表框中选择元素?这取决于你,我会给你代码,然后你只需改变你需要的东西。

您的元素具有以下结构:

  

一个
  A,B
  A,B,C

在这种情况下,A topWidget B为chassis, display, powerSupply,C为mainCase, bolt, screen, displayElectronics... etc,您可以选择要删除的内容,换句话说,如果删除{{1}您将删除所有B element,因此您需要选择是否只删除C elements too或全部C element,您可以使用B elements执行此操作,你需要发送什么? switch但是,您的ListBox包含以下内容:The selected item from the ListBox对吗?然后你只需要进行拆分以获得24 - Bolt或其他任何内容。

Bolt

有了这个,你将得到你添加的相同字符串,你只需要处理var select = listParts.SelectedItem.ToString().IndexOf('-'); string selectVal = listParts.SelectedItem.ToString().Substring(select + 1).Trim(); 并使用switch函数,如下所示:

Remove

或者您可以执行此操作以删除switch (selectVal) { //This will remove all the elements of powerSupply (transformer, powerBoard ) case "Power Supply": topWidget.Remove(powerSupply); break; }

C element one by one

请记住,在切换后,您需要刷新ListBox switch (selectVal) { //This will remove only "Transformer". case "Transformer": powerSupply.Remove(transformer); break; }

你可以毫无问题地结合这两种情况,我希望这对你有帮助。