1

I have created a dynamic label on button click on Windows Form. And then on a right click on the label. I'm showing a context menu "cm". I obviously want to add functionality to the context menu items. But what I don't understand is how do I reference the "lbl" object inside the event handler? How can I edit the properties of the labels from inside the event handlers named MarkedImportant and EditLabel?

public void btnMonSub_Click(object sender, EventArgs e)
{
    string s = txtMonSub.Text;
    Label lbl = new Label();
    lbl.Text = s;
    lbl.Location = new System.Drawing.Point(205 + (100 * CMonSub), 111);
    CMonSub++;
    lbl.Size = new System.Drawing.Size(100, 25);
    lbl.BackColor = System.Drawing.Color.AliceBlue;
    this.Controls.Add(lbl);

    ContextMenu cm = new ContextMenu();
    cm.MenuItems.Add("Mark Important", MarkImportant);
    cm.MenuItems.Add("Edit", EditLabel );

    lbl.ContextMenu = cm;
}

private void MarkImportant(object sender, EventArgs e)
{
    // imp..
}

private void EditLabel(object sender, EventArgs e)
{
    // edit..
}

Or is there a better way to do this? Like dynamically adding the event handler itself?

Thanks in advance.

  • What is this event attached to? It appears to be an event on a button, but you are wanting to access a label associated with that button. You can either access the label by its instance, or if you need it to be more dynamic, you would need to associate the label with the button. That could be done in a number of ways. The best way would entirely rely on how your application is designed an what requirements you have. – gmiley Apr 23 '16 at 18:28
  • You can retrieve it via `object sender`. Have a look at [this extensive answer](http://stackoverflow.com/a/4886417/594832) which describes it very well. – Jan Köhler Apr 23 '16 at 18:28
  • @khlr not possible. The event handler is attached to a ContextMenu, the sender is the ContextMenu. – Steve Apr 23 '16 at 18:32

1 Answers1

1

The ContextMenu has a property called SourceControl and MSDN says it

Gets the control that is displaying the shortcut menu.

So your event handler could reach the ContextMenu from the MenuItem passed as the sender parameter in this way

private void MarkImportant(object sender, EventArgs e)
{
    // Convert the sender object to a MenuItem 
    MenuItem mi = sender as MenuItem;
    if(mi != null)
    {
        // Get the parent of the MenuItem (the ContextMenu) 
        // and read the SourceControl as a label
        Label lbl = (mi.Parent as ContextMenu).SourceControl as Label;
        if(lbl != null)
        {
            ....
        }
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Could you explain what is the if statement actually checking for? And I just need to edit the label. Edit the label on which the user has right clicked. Basically change the properties of that particular label. – Archisman Dinda Apr 23 '16 at 18:42
  • Preventing `NullReferenceException`s – gmiley Apr 23 '16 at 18:43
  • 1
    It is an habit. In this context probably every object reference present will never be null, but adding a simple check for nulls is a lifesaver that you learn to use when you're on the wrong end of a phone call with an angry customer – Steve Apr 23 '16 at 19:45