[英]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
}
}
}
}
Here is a step by step example:这是一个分步示例:
Form
Form
TableLayoutPanel
from toolbox on your Form
TableLayoutPanel
放在Form
tableLayoutPanel1
on design surface and Press F4 Key to see properties.tableLayoutPanel1
并按F4键查看属性。CellPaint
event to create tableLayoutPanel1_CellPaint
event handler in code.CellPaint
事件以在代码中创建tableLayoutPanel1_CellPaint
事件处理程序。e.Row
is the row index, e.Column
is column index and e.CellBounds
is bound of the painting cell.e.Row
, e.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:要更改
Cell
的BackColor
,您可以:
private void Button1_Click(object sender, EventArgs e)
{
//column: 0 ,row: 1
bgColors[0, 1] = Color.Red;
tableLayoutPanel1.Refresh();
}
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
放在每个单元格中,并将Panel
的Dock
属性设置为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;
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.