TableLayoutPanel - get cell clicked

J

JamesB

Hello, I had posted this to the C# group but then realised here might be
more relevant.

This seems like it should be simple - on the double-click event of a
tablelayoutpanel, I want to know which cell was double clicked in. I can't
see any of the properties that look relevant to this, could someone give me
a pointer where to look? I can see various properties to give me the cell of
a certain control and so on, but some cells might not have a control in yet
(by double clicking the user will instigate the creation of a control in
that cell - or at least that's the plan!)
Thanks!
James
 
A

AHadiA

Hi James,
You can use MouseDoubleClick and GetWidths & GetHeights methods :


private void tableLayoutPanel1_MouseDoubleClick(object sender,
MouseEventArgs e)
{
int[] widths=tableLayoutPanel1.GetColumnWidths();
int[] heights = tableLayoutPanel1.GetRowHeights();

int col = -1;
int left = e.X;
for(int i=0;i<widths.Length;i++)
{
if (left < widths)
{
col = i;

break;
}
else
left -= widths;
}

int row = -1;
int top = e.Y;
for (int i = 0; i < heights.Length; i++)
{
if (top < heights)
{
row = i;
break;
}
else
top -= heights;
}

MessageBox.Show(string.Format( "Col {0},Row {1}",col,row));
}

Best Regards,
A.Hadi
 
J

JamesB

Hi James,
You can use MouseDoubleClick and GetWidths & GetHeights methods :


private void tableLayoutPanel1_MouseDoubleClick(object sender,
MouseEventArgs e)
{
int[] widths=tableLayoutPanel1.GetColumnWidths();
int[] heights = tableLayoutPanel1.GetRowHeights();

int col = -1;
int left = e.X;
for(int i=0;i<widths.Length;i++)
{
if (left < widths)
{
col = i;

break;
}
else
left -= widths;
}

int row = -1;
int top = e.Y;
for (int i = 0; i < heights.Length; i++)
{
if (top < heights)
{
row = i;
break;
}
else
top -= heights;
}

MessageBox.Show(string.Format( "Col {0},Row {1}",col,row));
}

Best Regards,
A.Hadi


Many thanks for that, will take a look at it later on today!
James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top