无法看到子菜单,只有最后一个subChild [子菜单]对于opChild是可见的 [英] unable to see sub Menus, only last subChild [Sub Menu] is visible for opChild

查看:80
本文介绍了无法看到子菜单,只有最后一个subChild [子菜单]对于opChild是可见的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试在鼠标单击上创建动态菜单,我可以创建父菜单和子菜单,但我无法创建subChlid菜单。只有最后一个subChild菜单,即OutPut 6可见。

我是C#的新手,我向Code Project寻求帮助。这是否发生,因为只有一个subChild实例。但我不想创建subChild的多个实例。欢迎提出所有建议。



Hi I am trying to create dynamic menu on mouse click, I am able to create Parent menu and Child menu, but I am not able to create subChlid menu. Only the last subChild menu i.e. "OutPut 6" is visible.
I am new to C# and I refer Code Project for help. Is this happening because there is only one instance of subChild. But I don't want to create multiple instance of subChild. All Suggestions are welcome.

void lstView_MouseClick(object sender, MouseEventArgs e)
{
    //throw new NotImplementedException();
    switch(e.Button)
    {
        case System.Windows.Forms.MouseButtons.Right:

            MenuItem[] subChild = new MenuItem[3];
            subChild[0] = new MenuItem("Digital", IO_Click);
            subChild[1] = new MenuItem("Analog", IO_Click);
            subChild[2] = new MenuItem("Special", IO_Click);

            MenuItem[] parent = new MenuItem[2];
            parent[0] = new MenuItem("InPut", IO_Click);
            parent[1] = new MenuItem("OutPut", IO_Click);

            MenuItem[] ipChild = new MenuItem[9];
            for (int ip = 0; ip < 9; ip++)
            {
                ipChild[ip] = new MenuItem("Input " + (ip + 1).ToString(), IO_Click);
                ipChild[ip].MenuItems.Add(subChild[0]);
                ipChild[ip].MenuItems.Add(subChild[1]);
                ipChild[ip].MenuItems.Add(subChild[2]);
                parent[0].MenuItems.Add(ipChild[ip]);
            }

            MenuItem[] opChild = new MenuItem[6];
            for (int op = 0; op < 6; op++)
            {
                opChild[op] = new MenuItem("Output " + (op + 1).ToString(), IO_Click);
                opChild[op].MenuItems.Add(subChild[0]);
                opChild[op].MenuItems.Add(subChild[1]);
                opChild[op].MenuItems.Add(subChild[2]);
                parent[1].MenuItems.Add(opChild[op]);
            }
            /* List View for Menu in List View */
            lstViewMenu = new ContextMenu(parent);
            lstViewMenu.Show(lstView, e.Location);

            break;
        default:
            break;
    }
}

推荐答案

我相信这是因为MenuItem只能属于一个单一的父MenuItem(虽然我不是100%肯定 - MSDN在 http://msdn.microsoft.com/en-us//library/system.windows.forms.menuitem%28v=vs.110%29.aspx [ ^ ],所以它需要拆卸才能确定)。但由于它有 Parent 属性,它引用了父菜单项,我认为这是真的。如果菜单项只能有单个父项,则表示每次将此菜单项添加到另一个菜单时,所述项与之前所属的父项分离。

所以,修复你的问题,我建议以下之一:

1)把

I believe it's because MenuItem can only belong to one single parent MenuItem (though I'm not 100% positive - MSDN doesn't mention anything about that in http://msdn.microsoft.com/en-us//library/system.windows.forms.menuitem%28v=vs.110%29.aspx[^], so it requires disassembling to be sure). But since it has Parent property, which refers to the parent menu item, I think it's true. If menu item can only have single parent, it means every time you add this menu item to another menu, the said item is "detached" from the parent it previously belonged to.
So, to fix your issue, I'd propose one of the following:
1) Put
MenuItem[] subChild = new MenuItem[3];
subChild[0] = new MenuItem("Digital", IO_Click);
subChild[1] = new MenuItem("Analog", IO_Click);
subChild[2] = new MenuItem("Special", IO_Click);



阻止 opChild inChild的两个周期







2)使用 CloneMenu() 创建副本的方法当前对象的例子,例如


block in both of the cycles for opChild and inChild:



2) Use CloneMenu() method which creates a duplicate of the current object, e.g.

ipChild[ip].MenuItems.Add(subChild[0].CloneMenu());





UPD :我发现MSDN实际上在 CloneMenu()中提到了这种重新启动/ code>方法说明:除非您获得MenuItem的副本,否则不能在多个地方使用MenuItem对象。此外,由于 CloneMenu()执行深度复制(即也创建了所有子菜单的副本),我建议使用更少数组的以下代码:



case System.Windows.Forms.MouseButtons.Right:





UPD: I discovered that MSDN actually mentions this restrinction in CloneMenu() method description: MenuItem objects cannot be used in more than one place unless you obtain a copy of the MenuItem. Also, since CloneMenu() performs a deep copying (i.e. also creates a copy of all the submenus), I propose the following code with less arrays:

case System.Windows.Forms.MouseButtons.Right:

            case System.Windows.Forms.MouseButtons.Right:

    MenuItem pTypeSubmenu = new MenuItem(string.Empty, IO_Click);

    pTypeSubmenu.MenuItems.Add(new MenuItem("Digital", IO_Click));
    pTypeSubmenu.MenuItems.Add(new MenuItem("Analog", IO_Click));
    pTypeSubmenu.MenuItems.Add(new MenuItem("Special", IO_Click));

    MenuItem[] parent = new MenuItem[2];
    parent[0] = new MenuItem("InPut", IO_Click);
    parent[1] = new MenuItem("OutPut", IO_Click);

    MenuItem pSubmenu = null;
    for (int ip = 0; ip < 9; ip++)
    {
        pSubmenu = pTypeSubmenu.CloneMenu();
        pSubmenu.Text = "Input " + (ip + 1).ToString();
        parent[0].MenuItems.Add(pSubmenu);
    }

    for (int op = 0; op < 6; op++)
    {
        pSubmenu = pTypeSubmenu.CloneMenu();
        pSubmenu.Text = "Output " + (op + 1).ToString();
        parent[1].MenuItems.Add(pSubmenu);
    }
    /* List View for Menu in List View */
    lstViewMenu = new ContextMenu(parent);
    lstViewMenu.Show(lstView, e.Location);

    break;
default:
    break;


这篇关于无法看到子菜单,只有最后一个subChild [子菜单]对于opChild是可见的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆