找到动态创建的menustrip项目的子菜单

时间:2014-10-14 06:05:36

标签: c# .net winforms c#-4.0 dynamic

请看这张图片......

enter image description here

我有一个带有银行项目的菜单条,它有动态创建的子菜单,每个子菜单都有付款&存款和每笔付款和现金都有Check&现金。

如果我点击现金SBI->存款 - >现金它应该说它来自哪里,因为我对所有银行都有相同的表格,请告诉我如何才能找到它

动态创建菜单

ToolStripMenuItem bankname = new ToolStripMenuItem(dr["bankabbr"].ToString());
            if (Convert.ToInt32(dr["isdeposit"]) == 1)
            {
                ToolStripMenuItem deposit = new ToolStripMenuItem("Deposit"); 
                if (Convert.ToInt32(dr["hascashindeposit"]) == 1)
                {
                    ToolStripMenuItem dcash = new ToolStripMenuItem("Cash");
                    dcash.Click += new EventHandler(dcash_Click);
                    deposit.DropDownItems.Add(dcash);
                    dcash.Name = "dcash";
                }
                if (Convert.ToInt32(dr["haschequeindeposit"]) == 1)
                {
                    ToolStripMenuItem dcheque = new ToolStripMenuItem("Cheque");
                    dcheque.Click += new EventHandler(dcheque_Click);
                    deposit.DropDownItems.Add(dcheque);

                }
                bankname.DropDownItems.Add(deposit);
            }

1 个答案:

答案 0 :(得分:0)

ToolStripMenuItem有一个名为OwnerItem的属性。您可以爬到已知的根项目或直到您到达地带并收集您的项目名称。

我假设你的根是ToolStrip

private void dcash(object sender, EventArgs e)
{
    ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
    List<string> path = new List<string>();
    path.Add(tsmi.Name);
    while (tsmi.OwnerItem.GetType() != typeof(ToolStrip))
    {
         tsmi = (ToolStripMenuItem)tsmi.OwnerItem;
         path.Add(tsmi.Name);
    }
}

使用您的示例将导致 Path[0] = "Cash" Path[1] = "Desposit" Path[2] = "SBI"