Hi,

I am dynamically creating some labels with a contextmenu associated with them and I am having few menuitems also in the context menu. The context menu will be called when a label right-clicked( or clicked-hold) I have written an evenhandler to be called when a menu item is clicked. Once the eventhandler is called I want to get the text of the label from which the menu was invoked. Any idea how to get it?

For example

Let the label be "LABEL"


LABEL ----(RightClick)------>CONTEXT-MENU----(ItemClick)------>Event(I want to get the LABEL.text in this event)


-anoop

Okay that looks a little bit confusing. But I think you can get it done like this:

Firstly declare a class-level variable "ClickedLabel":

// Declare here
Label ClickedLabel = null;

public Form1()
{
   InitializeComponent();
}

Secondly add "MouseUp" event for the label OR make an event "AllLabels_MouseUp" and set that for all the labels. And use this code:

private void AllLabels_MouseUp(object sender, MouseEventArgs e)
{
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
                LabelClicked = (sender as Label);
}

And you can access the label from the "Context Menu --> Item Click" event like this:

// An example for getting the Text
string labelText = ClickedLabel.Text

Hope that helps.

Thanks

Hi,

Thanks for the quick reply
I tried like this

lblColor.MouseUp += new MouseEventHandler(lblColor_MouseUp);
void lblColor_MouseUp(object sender, MouseEventArgs e)
        {
            //IMPLEMENTATION
        }

but the event was never called
I already have a lblColor.Click event in place, but I cant use for my purpose since when you right click it wont be called.

-Anoop

You might be making some mistake. The MouseUp event is always called (after Click event if its fired).

Can you post your code please?

Thanks