Click here to Skip to main content
16,017,650 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

How can I know what cell was clicked in the TableLayoutPanel in the click event?

Example: We can only get the position of the button on the TableLayoutPanel like this

MessageBox.Show( (tableLayoutPanel1.GetCellPosition(button1)).ToString());

The answer is (0,1).

But I want to know just the position of the cell without adding a button or something on the panel.
Posted
Updated 24-Nov-10 7:48am
v2

You can get the location like this

  private void tableLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
    {
        int row = 0;
        int verticalOffset = 0;
        foreach (int h in tableLayoutPanel1.GetRowHeights())
        {
            int column = 0;
            int horizontalOffset = 0;
            foreach(int w in tableLayoutPanel1.GetColumnWidths())
            {
                Rectangle rectangle = new Rectangle(horizontalOffset, verticalOffset, w, h);
                if(rectangle.Contains(e.Location))
                {
                    Trace.WriteLine(String.Format("row {0}, column {1} was clicked", row, column));
                    return;
                }
                horizontalOffset += w;
                column++;
            }
            verticalOffset += h;
            row++;
        }
    }
 
Share this answer
 
Hi there,

TableLayoutPanels don't really have 'cells' as such and are really meant to be a container for controls. This means you can't really retrieve individual rows, columns or cells. An alternative would be to use panels and put individual click events on each of these.

However if you're determined that you want to use TableLayoutPanels, you can use the XY coordinate of where the mouse click occured on the TLP from EventArgs. You can define a range of points to be the rows/columns within them using if statements. If you've got evenly spread rows/columns, you can use a loop to do this.

For example, if the click occured where X = 40 and Column1 has range 0-50, 0<x>
 
Share this answer
 
You can use GetColumnWidths and GetRowHeights methods to calculate the cell row and column index:

Point? GetRowColIndex(TableLayoutPanel tlp, Point point)
{
if (point.X > tlp.Width || point.Y >tlp.Height)
return null;

int w = tlp.Width;
int h = tlp.Height;
int[] widths = tlp.GetColumnWidths();

int i;
for (i = widths.Length - 1; i > 0 && point.X < w; i--)
w -= widths[i];
int col = i + 1;

int[] heights = tlp.GetRowHeights();
for (i = heights.Length - 1; i >= 0 && point.Y < h; i--)
h -= heights[i];

int row = i + 1;

return new Point(col, row);
}

Usage:

private void tableLayoutPanel1_Click(object sender, EventArgs e)
{
var cellPos = GetRowColIndex(
tableLayoutPanel1,
tableLayoutPanel1.PointToClient(Cursor.Position));
}
 
Share this answer
 
v2
Comments
CHill60 19-Jun-15 6:16am    
You're over 4 years late with this solution that is a straight copy from this SO post[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900