Search code examples
c#winformstoolstripmenu

How to determine the parent of child menu item in ToolStripMenu?


I am a hobby programmer. In my C# 2010 Express + SQL Server 2008 desktop application I am trying to add code to click event of childitem. I want to determine the parent (menuitem) of this child menu item. How can I find it?


Solution

  • Try like this

    private void mnuDatabase1_Click(object sender, ...)
    {
        ToolStripMenuItem MyMenuItem = (ToolStripMenuItem)sender;
        ToolStripMenuItem parent = (ToolStripMenuItem)MyMenuItem.OwnerItem;
        if (parent.Name == "mnuAdd")
            //Add Menu
        else if (parent.Name == "mnuEdit")
            //Edit Menu
        else if (parent.Name == "mnuDelete")
            //Delete Menu
    }