Question

I have created a Table layout in windows forms as shown in figure, i have added a right mouse button click Menu to my table, i want to color the cell when i right click on the perticuler cell, so how can i do it.

When i click add device the cell should paint to green color, When i click delete device it should show default color, When i click fire the cell should be painted with red color so on

The below is my form and table layout

http://i48.tinypic.com/33ok3cw.jpg

Was it helpful?

Solution 2

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";

        }
    }

http://i.imgur.com/XkfHENW.png?1

OTHER TIPS

Hi there i dont have what you looking for but,

TableLayoutPanels don't really have 'cells' as such and are really meant to be a container for controls. This means you can't really retrieve individual rows, columns or cells. An alternative would be to use panels and put individual click events on each of these.

However if you're determined that you want to use TableLayoutPanels, you can use the XY coordinate of where the mouse click occurred on the TableLayoutPanel from EventArgs. And determine which block it is, since you've got evenly spread rows/columns.

For example if you have all the cells the same size and the the TableLayoutPanel is docked in the form this will get the selected Cell.:

Point selectedCell = new Point();
private void tableLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {

       //show contextMenuStrip
        selectedCell = new Point(e.X / (tableLayoutPanel1.Width / tableLayoutPanel1.ColumnCount), e.Y / (tableLayoutPanel1.Height / tableLayoutPanel1.RowCount));
    }
}

There is an easier way without having to implement those events as well as the extra variables. Assuming the contextmenu is named 'DeviceActionsContextMenu' and is linked to the label controls (as per your comment), Implement the toolstrip Item's click event -

    private void addDeviceToolStripMenuItem_Click(object sender, EventArgs e)
    {
        (contextMenuStrip1.SourceControl as Label).BackColor = Color.Green;
    }

Or Implement the contextmenu's Item clicked event -

    private void DeviceActionsContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        if (e.ClickedItem == addDeviceToolStripMenuItem)
        {
            (contextMenuStrip1.SourceControl as Label).BackColor = Color.Green;
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top