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?
Asked
Active
Viewed 2,826 times
0
-
You mean you want to determine that which menu has pressed to load this current form? or you want to get the parent menu item of the clicked menu strip item? – Shell Sep 08 '14 at 06:07
-
yes. I have menu called operation. in that submenus are Add, edit, delete, in the every submenu there is same database names to select. I want to know from which submenu I have selected the database. – Muni Sep 08 '14 at 06:11
-
Have you tried that solution? – Shell Sep 08 '14 at 06:25
-
yes. Getting error in second line of code-- Cannot implicitly convert type 'System.Windows.Forms.ToolStripItem' to 'System.Windows.Forms.ToolStripMenuItem'. An explicit conversion exists (are you missing a cast?) – Muni Sep 08 '14 at 06:49
1 Answers
2
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
}

Shell
- 6,818
- 11
- 39
- 70
-
Getting error in second line of code-- Cannot implicitly convert type 'System.Windows.Forms.ToolStripItem' to 'System.Windows.Forms.ToolStripMenuItem'. An explicit conversion exists (are you missing a cast?) – Muni Sep 08 '14 at 06:46
-
Sorry i have forgot to cast the OwnerItem to `ToolStripMenuItem` class object. – Shell Sep 08 '14 at 06:54
-
1Isn't that amazing. The error message *suggested the actual fix*. Why didn't you try that, Muni? – Cody Gray - on strike Sep 08 '14 at 07:06
-
sorry but i didn't get meaning of that. I tried declaring , ToolStripMenuItem OwnerItem = new ToolStripMenuItem. it didn't solve the error. – Muni Sep 08 '14 at 07:29
-
Cody Grey, Thanks, I went through old post. It solves my purpose partially. I will try myself further & get back if required. – Muni Sep 08 '14 at 08:22