简体   繁体   English

如何在 TableLayoutPanel 中动态设置单元格颜色?

[英]How to set cell color in TableLayoutPanel dynamically?

I need to write a function which will set the color in TableLayoutPanel cells depending on some condition during running the program.我需要编写一个函数,该函数将根据运行程序期间的某些条件在TableLayoutPanel单元格中设置颜色。

TableLayoutPanel is divided by 16x16. TableLayoutPanel被 16x16 分割。 There is some condition at the start of the program.程序开始时有一些条件。 If the condition is true for a cell this sell must be painted blue color.如果单元格的条件为真,则此出售必须涂成蓝色。 For example:例如:

private void start_Click(object sender, EventArgs e)
{
    foreach (string str in some_list)
    {
       if (some condition)
       {
           set_color_in_cell at row[i] colum[j] //(what shoud i use here?)
       }
    }
}

I found such example:我找到了这样的例子:

private void tableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    if (e.Row == 0 && e.Column == 1)
    {
        e.Graphics.FillRectangle(new SolidBrush(Color.Black), e.CellBounds);
    }
}

But I don't understand how to use it.但我不明白如何使用它。 If somebody knows about this please help me.如果有人知道这一点,请帮助我。

private void start_Click(object sender, EventArgs e)
{
    string SyncAnswer = "";
    foreach (string file_string in Data_from_file)
    {
       COM_Port.WriteLine(file_string);
       while (SyncAnswer != "READY")
       {
           SyncAnswer = COM_Port.ReadLine();
           if (SyncAnswer.Substring(0, 4) == "Fire")
           {
              //raise event
              //paint for example a cell in Row=i Colum=j
           }
           else if (SyncAnswer.Substring(0, 4) == "Skip")
          {
             //raise event
          }
      }
   }
}

Option 1 - Using CellPaint Event选项 1 - 使用CellPaint事件

Here is a step by step example:这是一个分步示例:

  1. Create a Form创建Form
  2. Put a TableLayoutPanel from toolbox on your Form将工具箱中的TableLayoutPanel放在Form
  3. Select tableLayoutPanel1 on design surface and Press F4 Key to see properties.在设计图面上选择tableLayoutPanel1并按F4键查看属性。
  4. From toolbar of property grid, you can select to show Properties从属性网格的工具栏中,您可以选择显示属性在此处输入图片说明 or Events或事件在此处输入图片说明 . . Click on events icon and from the list, double click on CellPaint event to create tableLayoutPanel1_CellPaint event handler in code.单击事件图标,然后从列表中双击CellPaint事件以在代码中创建tableLayoutPanel1_CellPaint事件处理程序。
  5. You can paint each cells background in this method based on some criteria.您可以根据某些标准在此方法中绘制每个单元格的背景。 The event will raise for painting each cells background and e.Row is the row index, e.Column is column index and e.CellBounds is bound of the painting cell.该事件将为绘制每个单元格的背景而e.Rowe.Row是行索引, e.Column是列索引, e.CellBounds是绘制单元格的边界。

For example in below sample, we draw black background if ((e.Column + e.Row) % 2 == 1) otherwise, we draw white background:例如在下面的示例中,我们绘制黑色背景if ((e.Column + e.Row) % 2 == 1)否则,我们绘制白色背景:

private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    if ((e.Column + e.Row) % 2 == 1)
        e.Graphics.FillRectangle(Brushes.Black, e.CellBounds);
    else
        e.Graphics.FillRectangle(Brushes.White, e.CellBounds);
}

在此处输入图片说明

To change Color Dynamically动态更改颜色

To change the color from another point of program, for example in a Click event of a button, you should store back colors of each cell in an 2-dimension array and use that color to create a brush for that cell:要从程序的另一个点更改颜色,例如在按钮的Click事件中,您应该将每个单元格的背景颜色存储在二维数组中,并使用该颜色为该单元格创建画笔:

Define bgColors in your form:在您的表单中定义bgColors

Color[,] bgColors = new Color[2, 2] {
    { SystemColors.Control, SystemColors.Control }, 
    { SystemColors.Control, SystemColors.Control } 
};

Draw background of cells this way:以这种方式绘制单元格背景:

private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    using (var b = new SolidBrush(bgColors[e.Column, e.Row]))
    {
        e.Graphics.FillRectangle(b , e.CellBounds);
    }
}

To change the BackColor of a Cell you can:要更改CellBackColor ,您可以:

private void Button1_Click(object sender, EventArgs e)
{
    //column: 0 ,row: 1
    bgColors[0, 1] = Color.Red;
    tableLayoutPanel1.Refresh();
}

Option 2 - Hosting Panel in Cells选项 2 - 在单元格中托管面板

As another simple option, you can put Panel in each cell, and set the Dock property of Panel to Fill and set its Margin property to 0,0 , then each time you want to change color of a panel at position (column, row) you can use this code:作为另一个简单的选项,您可以将Panel放在每个单元格中,并将PanelDock属性设置为Fill并将其Margin属性设置为0,0 ,然后每次要更改位置(column, row)面板的颜色您可以使用此代码:

this.tableLayoutPanel1.GetControlFromPosition(column, row).BackColor = Color.Red;

Another way that is pretty simple is to dynamically add multiple PictureBox controls (ex: pbCellColor below) to you cells in your TableLayoutPanel, or some other more simple simple control, dock it, set the margin to zero, then whenever you want:另一种非常简单的方法是向 TableLayoutPanel 中的单元格动态添加多个 PictureBox 控件(例如:下面的 pbCellColor),或者其他一些更简单的控件,停靠它,将边距设置为零,然后随时:

pbCellColor.Dock = DockStyle.None;
pbCellColor.Margin = new Size(0, 0, 0, 0);
pbCellColor.Backcolor = Color.Red;    

TableLayoutPanel 与 PictureBox 为单元格着色

Easy, no event handling or state checks.简单,无需事件处理或状态检查。 Just set it and forget it.只需设置并忘记它。 Worst case, if you call from a non-gui thread, you will need to Invoke the action.最坏的情况是,如果您从非 gui 线程调用,则需要调用该操作。

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

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