ContextMenuStrip menuItem.Name is always ""

rfresh

Active member
Joined
Aug 23, 2012
Messages
26
Programming Experience
1-3
My menu code fires the menu click event but the menuItem.Name is always ""

Obviously my code isn't setup right.

Thanks for any help.

Code:
            ContextMenuStrip menuStrip = new ContextMenuStrip();
            ToolStripMenuItem menuItem = new ToolStripMenuItem();
            menuStrip.Items.Add("1111");
            menuStrip.Items.Add("2222");
            menuItem.Click += new EventHandler(menuItem_Click);
            panelMenu.ContextMenuStrip = menuStrip;




        void menuItem_Click(object sender, EventArgs e)
        {
            ToolStripItem menuItem = (ToolStripItem)sender;
            if (menuItem.Name == "1111")
            {


            }
        }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,286
Location
Sydney, Australia
Programming Experience
10+
Have you read the relevant documentation? It would appear not. If you had read the appropriate documentation, or just taken notice of Intellisense as you typed, then you'd know that that Add method you're calling is described thusly:
Adds a ToolStripItem that displays the specified text to the collection.
Think about, for instance, a TextBox. How do you get the text it displays? Is it via the Name property?

When you encounter an issue, ALWAYS read the relevant documentation first. There's a Help menu right there in VS. Read the documentation for any types and members that you're using and make sure that they work the way you think they work. You'll find that you can then answer many of your questions yourself. I speak from personal experience. Forums are great and I use them myself, but never before reading the appropriate information first.
 
Top Bottom