views:

45

answers:

2

Hi, I have a TableLayoutPanel and every time I put a label into one of the cells, it snaps to the top left corner. How can I get to it not do this or change where it snaps.

Also, is it possible to change a specific cell's background color?

Thanks!

+3  A: 

Soo, you can control where a label 'snaps' in a cell by setting the Dock property on the label. It will dock within the confines of the cell. I don't believe you can change the background color of a cell. One way to work around this is to put a panel in each cell, set it to dock using the full cell, and set the back ground color of the panel.

Randy Minder
Is it possible to stick more than 1 lable in a cell? I seem to be unable to do so...
Soo
@Soo - I've never tried, but off-hand, I don't see why not. It might be easier to put a panel in a cell, and then put multiple controls on the panel.
Randy Minder
+1  A: 

To change the location of a control in a cell use the control's Anchor property.

To change the background color of a cell within a TableLayoutPanel, use the control's CellPaint event to test which Column and/or Row is being painted and set the color accordingly.

The following will set the background color of the cell at 1, 1 to red:

    private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) {
        if (e.Column == 1 && e.Row == 1) {
            e.Graphics.FillRectangle(Brushes.Red, e.CellBounds);
        }
    }
Jay Riggs
Is it possible to stick more than 1 lable in a cell? I seem to be unable to do so...
Soo
@Soo I like Randy's suggestion: put all the controls you want in a Panel and then put the Panel in your TLP.
Jay Riggs