我在 PictureBox 上创建了一个网格,如下所示:
private void PictureBoxPaint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int numOfCellsWidth = 50;
int numOfCellsHeight = 600;
int cellSize = 20;
Pen p = new Pen(Color.Black);
for (int y = 0; y < numOfCellsHeight; ++y)
{
g.DrawLine(p, 0, y * cellSize, numOfCellsHeight * cellSize, y * cellSize);
}
for (int x = 0; x < numOfCellsWidth; ++x)
{
g.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCellsHeight * cellSize);
}
}
我之前使用过 tableLayoutPanel,它有一个 CellPaint 事件,我可以将它绑定到一个数组列表,这样当列表改变时单元格的颜色就会改变。这就是我所拥有的:
private void tableLayoutPanelMainGrid_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
if (mainVisualization.mainGrid != null)
if (mainVisualization.mainGrid.cellList != null)
using (var b = new SolidBrush(mainVisualization.mainGrid.cellList[e.Column, e.Row].color))
e.Graphics.FillRectangle(b, e.CellBounds);
}
我怎样才能将这两者结合起来?