如何动态设置TableLayoutPanel中的单元格颜色? [英] How to set cell color in TableLayoutPanel dynamically?

查看:1423
本文介绍了如何动态设置TableLayoutPanel中的单元格颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个函数,该函数将在运行程序时根据某些情况在 TableLayoutPanel 单元格中设置颜色。



TableLayoutPanel 除以16x16。程序启动时存在一些条件。如果该条件对于某个单元格为真,则此销售必须涂成蓝色。例如:

  private void start_Click(对象发送者,EventArgs e)
{
foreach(字符串str在some_list中)
{
如果(在某些情况下)
{
set_color_in_cell在row [i] colum [j] ///(我在这里应该使用什么?)
}
}
}

我找到了这样的例子:

 私有void tableLayoutPanel_CellPaint(对象发送者,TableLayoutCellPaintEventArgs e)
{
if(e.Row == 0&& e.Column == 1)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Black),e.CellBounds);
}
}

但是我不知道如何使用它。

  private void start_Click(对象发送者,EventArgs e)
{
string SyncAnswer =;
foreach(Data_from_file中的字符串file_string)
{
COM_Port.WriteLine(file_string);
而(SyncAnswer!= READY)
{
SyncAnswer = COM_Port.ReadLine();
if(SyncAnswer.Substring(0,4)== Fire)
{
//引发事件
//绘制例如Row = i Colum = j
}
else if(SyncAnswer.Substring(0,4)==跳过)
{
//引发事件
}
}
}
}


解决方案

选项1-使用 或事件 。单击事件图标,然后从列表中双击



动态更改颜色



从程序的另一点更改颜色,例如按钮的 Click 事件,您应该将每个单元格的颜色存储在二维数组中,并使用该颜色为该单元格创建画笔:



在您的表单中定义 bgColors

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

以此方式绘制单元格背景:

 私有无效tableLayoutPanel1_CellPaint(对象发送者,TableLayoutCellPaintEventArgs e)
{
使用(var b = new SolidBrush(bgColors [e.Column,e.Row]))
{
e.Graphics.FillRectangle(b,e.CellBounds);
}
}

更改 BackColor 单元格,您可以:

 私有void Button1_Click(object sender,EventArgs e)
{
//列:0,行:1
bgColors [0,1] = Color.Red;
tableLayoutPanel1.Refresh();
}






选项2-主机面板在单元格中



作为另一个简单的选择,您可以在每个单元格中放置 Panel 并设置<$ c $将c> Panel 的c> Dock 停靠到 Fill 并设置其保证金属性设置为 0,0 ,然后每次您要更改位置的面板颜色(列,行)可以使用以下代码:

  this.tableLayoutPanel1.GetControlFromPosition(column,row).BackColor =颜色。红色; 


I need to write a function which will set the color in TableLayoutPanel cells depending on some condition during running the program.

TableLayoutPanel is divided by 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

Here is a step by step example:

  1. Create a Form
  2. Put a TableLayoutPanel from toolbox on your Form
  3. Select tableLayoutPanel1 on design surface and Press F4 Key to see properties.
  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.
  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.

For example in below sample, we draw black background if ((e.Column + e.Row) % 2 == 1) otherwise, we draw white background:

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:

Define bgColors in your form:

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:

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

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:

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

这篇关于如何动态设置TableLayoutPanel中的单元格颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆