ContextMenuStrip menuItem.Name is always ""


Active member
Aug 23, 2012
Programming Experience
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.

            ContextMenuStrip menuStrip = new ContextMenuStrip();
            ToolStripMenuItem menuItem = new ToolStripMenuItem();
            menuItem.Click += new EventHandler(menuItem_Click);
            panelMenu.ContextMenuStrip = menuStrip;

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

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