Row/Column coloring for TableLayoutPanel (vs2008, winform)

前端 未结 2 1208
梦毁少年i
梦毁少年i 2021-01-12 00:03

Can i add particular color for entire Row or Column in TableLayoutPanel ? How ? please provide sample code if any ..

Thanks in adv.

相关标签:
2条回答
  • 2021-01-12 01:01

    I found this answer much easier to implement:

    This allowed me to put a full backcolor on my cell.

    1. Create a Panel, which has a backcolor, and
    2. I Dock that Panel in my TableLayoutPanel

    Then that TableLayoutPanel Cell has a backcolor.

    My Code ended up looking like this:

    Panel backgroundColorPanel = new Panel();
    backgroundColorPanel.BackColor = Color.FromArgb(243, 243, 243);
    backgroundColorPanel.Dock = DockStyle.Fill;
    backgroundColorPanel.Margin = new Padding(0);
    backgroundColorPanel.Anchor = ((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left));
    backgroundColorPanel.AutoSize = true;
    backgroundColorPanel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
    this.originalTableLayoutPanel.Controls.Add(backgroundColorPanel, 0, row);
    

    http://www.codeguru.com/forum/showthread.php?t=444944

    0 讨论(0)
  • 2021-01-12 01:06

    Yes you can.

    Use the TableLayoutPanel's CellPaint event to test for which row/column has called the event and then use a Graphic object size to the rectangle to set the cell's color.

    Like this (for the first and third rows):

         private void Form_Load(object sender, EventArgs e) {
            this.tableLayoutPanel1.CellPaint += new TableLayoutCellPaintEventHandler(tableLayoutPanel1_CellPaint);
         }
    
    
        void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
        {
            if (e.Row == 0 || e.Row == 2) {
                Graphics g = e.Graphics;
                Rectangle r = e.CellBounds;
                g.FillRectangle(Brushes.Blue, r);
            }
        }
    
    0 讨论(0)
提交回复
热议问题