简体   繁体   English

如何控制动态创建的上下文菜单项中的子菜单项?

[英]How to control sub menu items in dynamically created contextmenustrip items?

I have this task to be done : there is a contextmenustrip, where in one of the branches there are 3 static items (created via designer) and after that there will be dynamically created items (links to subfolders of one folder on hard drive). 我要完成此任务:有一个contextmenustrip,其中一个分支中有3个静态项(通过设计器创建),然后将动态创建项(链接到硬盘驱动器上一个文件夹的子文件夹)。 Here it is part of the code i am using to create items : 这是我用来创建商品的代码的一部分:

    private void listBox1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right && listBox1.SelectedIndex != -1)
        {
            string pathDatosString = Path.Combine(PMVars.MainPath + clientsbox2.SelectedItem.ToString() + @"\" + listBox1.SelectedItem.ToString() + @"\01-Datos");

                int count = ((contextMenuStrip1.Items[2] as ToolStripMenuItem).DropDownItems[2] as ToolStripMenuItem).DropDownItems.Count;
                //MessageBox.Show(count.ToString());
                if (count > 3)
                {
                    for (int i = 3;i < count;i++)
                    {
                        ((contextMenuStrip1.Items[2] as ToolStripMenuItem).DropDownItems[2] as ToolStripMenuItem).DropDownItems.RemoveAt(i);
                    }

                }
            if (Directory.Exists(pathDatosString))
            {
                // This path is a directory
                foreach (string diritem in Directory.GetDirectories(pathDatosString))

                {
                    ToolStripMenuItem item = new ToolStripMenuItem();
                    item.Click += new EventHandler(OpenDir);
                    string dirCutted = diritem.Split('\\').Last();
                    ((contextMenuStrip1.Items[2] as ToolStripMenuItem).DropDownItems[2] as ToolStripMenuItem).DropDownItems.Add(dirCutted, null, OpenDir) ;
                }
            }
            contextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y);
        }

    }

So the problem i've encountered is that each time listbox1_mouseclick is being started - item is being created again and again (clones) So i tried to made a check if item with text already exists , but error pops out saying that collection has been changed. 所以我遇到的问题是每次启动listbox1_mouseclick时-一次又一次地创建项目(克隆),所以我尝试检查是否存在带有文本的项目,但弹出错误消息说集合已更改。 I think that's because of the dynamically created items i am collecting ? 我认为这是因为我正在收集动态创建的项目吗? This code works for deleting, but maybe there is more elegant solution for this? 该代码可用于删除,但也许对此有更优雅的解决方案?

Prior to adding the items, call the Clear() method of your DropDown. 在添加项目之前,请调用DropDown的Clear()方法。 You're scanning for folders everytime, so it makes sense to start with a clean slate. 您每次都在扫描文件夹,因此从一开始就很有意义。 Every point in the tree that has sub-items, and to which you add dynamically, is subject to this need to Clear() first. 树中具有子项并向其动态添加的每个点都必须首先满足Clear()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM