Results 1 to 2 of 2

Thread: ContextMenuStrip menuItem.Name is always ""

  1. #1
    rfresh is offline C# Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2012
    Posts
    19
    Rep Power
    59

    ContextMenuStrip menuItem.Name is always ""

    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")
                {
    
    
                }
            }

  2. #2
    jmcilhinney is offline C# Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,442
    Rep Power
    116
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •