I got the answer for my question it can be done as shown below First we need to find the label control(sender) which has sent the event as follows
private void AssignClickEvent()
{
foreach (Control c in tableLayout.Controls.OfType<Label>())
{
c.MouseClick += tablelayout_MouseClick;
addDevice.Click += addDevice_Click;
deleteDevice.Click += deleteDevice_Click;
fire.Click += fire_Click;
fault.Click += fault_Click;
suppress.Click += suppress_Click;
}
}
have a globle varible of pointer control
public Label h = new Label();
then we need to copy the sender to the control
private void tablelayout_MouseClick(object sender, MouseEventArgs e)
{
Label l = (Label)sender;
if (e.Button == MouseButtons.Right)
{
h = l;
num = l.Text;
m.MenuItems.AddRange(new MenuItem[] { addDevice, deleteDevice, fire, fault,suppress });
tableLayout.ContextMenu = m;
m.Show((Control)(sender), e.Location);
}
}
then use global control `h' to set color to the control
public void addDevice_Click(object sender, EventArgs e)
{
try
{
h.BackColor = Color.Green; ;
comport.Write("AB");
comport.Write(num);
comport.Write(" ");
stausLable.Text = "Device "+num+" added";
comport.WriteLine("000000000000");
}
catch (InvalidOperationException )
{
stausLable.Text = "Open communication port";
}
}