Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I used this code to set a backcolor for a specific cell in a tablelayoutpanel,
C#
private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
 
if (e.Column == GColumn && e.Row == GRow)//(GColumn=1 and Grow=1)
{
e.Graphics.FillRectangle(
Brushes.Pink , e.CellBounds);
}
}


Its working but i want to select more than one cells. So that, it should show backcolor for more than one cells. Need to select more than one cells with the help of ctrl button.

help me..
Posted
Updated 4-Dec-11 20:30pm
v2

1 solution

You could try this:

public partial class Form1 : Form
{
    List<TableLayoutCellConfig> tlcc = new List<TableLayoutCellConfig>();
    
    public Form1()
    {
        InitializeComponent();
        tlcc.Add(new TableLayoutCellConfig(0, 0, Brushes.Green));
        tlcc.Add(new TableLayoutCellConfig(0, 1, Brushes.Red));
        tlcc.Add(new TableLayoutCellConfig(1, 0, Brushes.Lime));
        

    }
    
    private void TableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
    {
        foreach (TableLayoutCellConfig conf in tlcc)
        {
            if (e.Row == conf.Row && e.Column==conf.Column)
            {
                e.Graphics.FillRectangle(conf.Brush, e.CellBounds);
                continue;
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        tlcc.Add(new TableLayoutCellConfig(1, 1, Brushes.Pink));
        tableLayoutPanel1.Invalidate();
    }

    
}

class TableLayoutCellConfig
{
    public TableLayoutCellConfig(int X, int Y, Brush Background)
    {
        this.Column = X;
        this.Row = Y;
        this.Brush = Background;
    }
    
    private int row;
    public int Row
    {
        get { return row; }
        set { row = value; }
    }

    private int column;
    public int Column
    {
        get { return column; }
        set { column = value; }
    }

    private Brush brush;
    public Brush Brush
    {
        get { return brush; }
        set { brush = value; }
    }


}


With
List<TableLayoutCellConfig>
you can iterate through your config during
TableLayoutPanel1_CellPaint


Don't forget to invalidate after changing config.
 
Share this answer
 
v2
Comments
Dalek Dave 5-Dec-11 5:02am    
Good Answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900