views:

766

answers:

2

I have a form which users can add controls to and when they right click it brings up a context menu which has an option to display the properties of the control they right clicked on. The problem I am having is trying to decide how to find out what control the user right clicks on since it could be any number of them and then giving that control focus on the form. Does anyone have any good suggestions? Should I just use the (object Sender) portion of a mouse event?

Thanks.

A: 

You could either check the type of the control that triggers the event using:

if (typeof(sender) == _control1.GetType())
{
     // ...
}

or assigning the Tag property of each control, and checking that in the event handler. Tag allows more customization, but does have the performance issue of (un)boxing. Then again, checking the type might just be as bad, but I have nothing to back that up.

nasufara
+2  A: 

My choice will be using object Sender, most straight forward. Though you need to do casting if you want to have operations on the specific control type.

UPDATE:

If you have a good naming convention or at least for those form controls which need ContextMenu operations, here's how you can do it:

Attaching mouse click event to specific controls or you can write something to attach to all controls by iteration through the form's Controls collection.

label1.MouseClick += new MouseEventHandler(control_RightMouseClick);
label2.MouseClick += new MouseEventHandler(control_RightMouseClick);
label3.MouseClick += new MouseEventHandler(control_RightMouseClick);

Then perform different operations or show different context menu for different controls

void control_RightMouseClick(object sender, MouseEventArgs e)
{
    if (e.Button != MouseButtons.Right)
    {
        return;
    }
    if (sender.GetType().IsSubclassOf(typeof(Control)))
    {
        Control formControl = (Control)sender;
        switch (formControl.Name)
        {
            case "label_1":
                //do something
                contextMenuStrip1.Show(formControl, e.Location);
                break;
            case "label_2":
                //do something else
                contextMenuStrip2.Show(formControl, e.Location);
                break;
            case "label_3":
                //do something else
                contextMenuStrip3.Show(formControl, e.Location);
                break;
            case "panel_1":
                //do something else
                break;
            default:
                //do something else or return or show default context menu
                contextMenuStrip_default.Show(formControl, e.Location);
                break;
        }
    }

    return;
}
o.k.w
If I have say 3 labels on my form and I use object Sender is Label and then enable focus will it place focus on all the labels or only the one clicked? I guess my main concern is having multiple controls of the same type and not being able to differentiate between them when they clicked on. Does that make sense?
Nathan
@Nathan: I've updated my answer to address your comments. Please refer to it.
o.k.w
@Nathan: darkassassin93's idea of using `Tag` can be used in place of the control name. But like he also mentioned, you'll need to cast the tag object.
o.k.w