表格单元格重绘事件可用于在运行时更改单元格颜色。
下面的例子是创建颜色数组来保存表格中每个单元格的颜色,并使用单元格重绘事件来绘制它。
每次点击按钮都会改变随机选择的单元格的颜色。
Form1.cs
namespace WindowsFormsTable
{
public partial class Form1 : Form
{
private Color[,] cellColors = null;
private Random rnd = new Random();
public Form1()
{
InitializeComponent();
// Prepare random colors for table cells
var columns = tableLayoutPanel1.ColumnCount;
var rows = tableLayoutPanel1.RowCount;
cellColors = new Color[columns, rows];
for (int c = 0; c < columns; c++)
{
for (int r = 0; r < rows; r++)
{
// Get random color
cellColors[c, r] = GetRandomColor();
}
}
}
// Table cell is redrawn event handler
private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
if (cellColors != null)
{
var color = cellColors[e.Column, e.Row];
e.Graphics.FillRectangle(new SolidBrush(color), e.CellBounds);
}
}
private void button1_Click(object sender, EventArgs e)
{
// Change color for randow cell
var column = rnd.Next(0, tableLayoutPanel1.ColumnCount - 1);
var row = rnd.Next (0, tableLayoutPanel1.RowCount-1);
cellColors[column, row] = GetRandomColor();
tableLayoutPanel1.Refresh();
// To change color of the top left cell remove code above and uncomment the following code line:
// cellColors[0, 0] = Color.FromArgb( /* define you color */ );
}
private Color GetRandomColor()
{
return Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));
}
}
}
Form1.Designer.cs
namespace WindowsFormsTable
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tableLayoutPanel1.ColumnCount = 4;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.Location = new System.Drawing.Point(12, 46);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 4;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(410, 256);
this.tableLayoutPanel1.TabIndex = 1;
this.tableLayoutPanel1.CellPaint += new System.Windows.Forms.TableLayoutCellPaintEventHandler(this.tableLayoutPanel1_CellPaint);
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(89, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Change Color";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(434, 314);
this.Controls.Add(this.button1);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Button button1;
}
}