【问题标题】:Menu items code optimization菜单项代码优化
【发布时间】:2016-07-17 02:39:10
【问题描述】:

我有一个包含 5 个子项的菜单项,每个子项包含 4 个子子项,每个子项都采用一种新形式,如果我想为每个需要采用的项使用新形式进行编码我 4*5 = 20 个表格!!!!

有什么方法可以让我知道所选子项的位置吗?然后我只能制作一种形式来制作一切

【问题讨论】:

  • 是的。将所有项目放在表单上,​​然后对控件使用 Visible = false(或 true)属性。
  • 这只是隐藏项目,我想获取所选项目的索引或类似的东西,这样我就可以根据所选项目索引以一种形式处理所有选择

标签: c# menuitem submenu


【解决方案1】:

如果您想从处理程序获取菜单项的位置,您可以继续处理每个所有者项并找到它的位置,直到获得完整的位置列表:

// Get a reference to the current item as a tool strip menu item
ToolStripMenuItem self = (ToolStripMenuItem)sender;

// Build a list of positions
List<int> position = new List<int>();
ToolStripMenuItem cur = self;
// Keep looping until we don't find a parent
while (cur != null)
{
    if (cur.OwnerItem is ToolStripMenuItem)
    {
        // The owner is a menu item, add it's position to our list
        ToolStripMenuItem parent = ((ToolStripMenuItem)cur.OwnerItem);
        position.Insert(0, parent.DropDownItems.IndexOf(cur));
        // And now work on the owner
        cur = parent;
    }
    else
    {
        // The owner isn't a menu item, so break out of our loop
        cur = null;
    }
}

// And as a demo, just show the positions:
MessageBox.Show("You clicked on item at " +
    string.Join(",", position.Select(x => x.ToString()).ToArray()));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    相关资源
    最近更新 更多