简体   繁体   English

在 WinForm C# 中单击按钮时更改表格单元格颜色

[英]Change table cell color when a button is clicked in WinForm C#

I am very new to Windows Forms, have been searching for an answer for three days already but no luck.我是 Windows 窗体的新手,已经搜索了三天的答案,但没有运气。

I have a button and a table with multiple cells.我有一个按钮和一个包含多个单元格的表格。 在此处输入图片说明

When the button is clicked, I need to change the color of the top left cell (index [0, 0]), but I don't understand how to do it since the button's on-click event method doesn't have TableLayoutCellPaintEventArgs element type parameter.单击按钮时,我需要更改左上角单元格的颜色(索引 [0, 0]),但我不明白该怎么做,因为按钮的点击事件方法没有 TableLayoutCellPaintEventArgs 元素类型参数。

Please advise how to do it.请指教怎么做。

The table cell redrawn event can be used to change cells color at the run-time.表格单元格重绘事件可用于在运行时更改单元格颜色。

The example below is creating color array to saving color for each cell in the table and using the cell redrawn event to draw it.下面的例子是创建颜色数组来为表格中的每个单元格保存颜色并使用单元格重绘事件来绘制它。

Each click on the button change color of the randomly selected cell.每次单击按钮都会更改随机选择的单元格的颜色。

Form1.cs表格1.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 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;
    }
}

在此处输入图片说明

Thanks to Jackdaw's answer I was able to figure our a solution that worked for me:感谢 Jackdaw 的回答,我找到了一个对我有用的解决方案:

I am changing colors of the table cells in tableLayoutPanel1_CellPaint event handler, and then calling it from the button's event handler using this script:我正在更改 tableLayoutPanel1_CellPaint 事件处理程序中表格单元格的颜色,然后使用以下脚本从按钮的事件处理程序中调用它:

this.tableLayoutPanel1.CellPaint += new TableLayoutCellPaintEventHandler(tableLayoutPanel1_CellPaint); this.tableLayoutPanel1.CellPaint += new TableLayoutCellPaintEventHandler(tableLayoutPanel1_CellPaint);

I was missing tableLayoutPanel1.Refresh();我错过了 tableLayoutPanel1.Refresh(); part, but after Jackdaw's answer I noticed and was able to add it.部分,但在寒鸦的回答之后,我注意到并能够添加它。 Everything is working correctly now.现在一切正常。

The full script looks something like this:完整的脚本如下所示:

    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();
    }

Thank you for your help!感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM