【发布时间】:2015-12-03 11:12:57
【问题描述】:
我需要编写一个函数,在程序运行期间根据某些条件设置TableLayoutPanel 单元格中的颜色。
TableLayoutPanel 除以 16x16。程序开始时有一些条件。如果单元格的条件为真,则此销售必须涂成蓝色。例如:
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?)
}
}
}
我找到了这样的例子:
private void tableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
if (e.Row == 0 && e.Column == 1)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Black), e.CellBounds);
}
}
但我不明白如何使用它。如果有人知道这件事,请帮助我。
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
}
}
}
}
【问题讨论】:
-
将
tableLayoutPanel_CellPaint分配给您的CellPaint事件tableLayoutPanel,然后根据某些标准进行绘制。e.Row是行索引,e.Column是单元格的列索引。e.CellBounds是单元格的绑定。 -
不幸的是我不擅长编程。您能否通过添加示例来澄清您的确切含义?
-
您可以使用 CellPaint 事件,也可以简单地将面板放入单元格中并更改面板的背景颜色。请参阅下面的答案。
标签: c# .net winforms tablelayoutpanel