0
我想点击我的tableLayoutPanel中最后一个单元格时出现问题。 当我运行一个程序,它看起来利卡这样的: enter image description here我无法点击表格布局面板中的最后一个单元格
下一页当我点击我看到最后一个单元格,一切都很好: enter image description here
但是当我滚动TableLayoutPanel中的点击在最后的最后单元格,它不标记最后一个单元格,但它在滚动TLP之前标记最后一个单元格。
这里是我的代码:
private void tableLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
{
row = 0;
int verticalOffset = 0;
foreach (int h in tableLayoutPanel1.GetRowHeights())
{
column = 0;
int horizontalOffset = 0;
foreach (int w in tableLayoutPanel1.GetColumnWidths())
{
Rectangle rectangle = new Rectangle(horizontalOffset, verticalOffset, w, h);
if (rectangle.Contains(e.Location))
{
if (column == 1) return;
Point cell = new Point(column, row);
if (!clickedCells.Contains(cell))
{
clickedCells.Add(cell);
}
else
{
clickedCells.Remove(cell);
}
tableLayoutPanel1.Invalidate();
MessageBox.Show(String.Format("row {0}, column {1} was clicked", row, column));
return;
}
horizontalOffset += w;
column++;
}
verticalOffset += h;
row++;
}
}
似乎'e.Location'是一个实际的屏幕上,从偏移面板的左上角,而代码则计算布局面板坐标中的偏移量。因此,e.Location永远不会大到指向最初看不见的行。也许有一些属性,如https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.horizontalscrollingoffset(v=vs.110).aspx面板你使用。在最坏的情况下,您可以将一个单独的面板放入每个单元格中,并将其“标记”属性设置为其位置。 –
然后,您可以使用相同的事件处理程序来处理每个面板的点击。 –