在 WinForm C# 中单击按钮时更改表格单元格颜色
Posted
技术标签:
【中文标题】在 WinForm C# 中单击按钮时更改表格单元格颜色【英文标题】:Change table cell color when a button is clicked in WinForm C# 【发布时间】:2021-02-22 08:45:18 【问题描述】:我是 Windows 窗体的新手,已经寻找了三天的答案,但没有运气。
我有一个按钮和一个包含多个单元格的表格。
单击按钮时,我需要更改左上角单元格的颜色(索引 [0, 0]),但我不明白该怎么做,因为the button's on-click event method 没有 TableLayoutCellPaintEventArgs 元素类型参数。
请告知如何做。
【问题讨论】:
这能回答你的问题吗? How to set cell color in TableLayoutPanel dynamically? 您现在需要展示如何绘制单元格。从按钮单击事件处理程序中,您必须以某种方式与绘画代码进行通信:将某个变量设置为某个值并强制重新绘画,检查绘画代码中的该变量以改变颜色。 【参考方案1】:表格单元格重绘事件可用于在运行时更改单元格颜色。
下面的例子是创建颜色数组来保存表格中每个单元格的颜色,并使用单元格重绘事件来绘制它。
每次点击按钮都会改变随机选择的单元格的颜色。
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;
【讨论】:
【参考方案2】:感谢 Jackdaw 的回答,我能够找到适合我的解决方案:
我在 tableLayoutPanel1_CellPaint 事件处理程序中更改表格单元格的颜色,然后使用此脚本从按钮的事件处理程序调用它:
this.tableLayoutPanel1.CellPaint += new TableLayoutCellPaintEventHandler(tableLayoutPanel1_CellPaint);
我缺少 tableLayoutPanel1.Refresh();部分,但在寒鸦的回答之后,我注意到并能够添加它。现在一切正常。
完整的脚本如下所示:
private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
if (e.Row == 0 && e.Column == 2)
e.Graphics.FillRectangle(Brushes.Red, e.CellBounds);
private void button1_Click(object sender, EventArgs e)
this.tableLayoutPanel1.CellPaint += new TableLayoutCellPaintEventHandler(tableLayoutPanel1_CellPaint);
tableLayoutPanel1.Refresh();
感谢您的帮助!
【讨论】:
以上是关于在 WinForm C# 中单击按钮时更改表格单元格颜色的主要内容,如果未能解决你的问题,请参考以下文章