如何知道TableLayoutPanel中单击的单元格 [英] How can I know the clicked cell in the TableLayoutPanel

查看:337
本文介绍了如何知道TableLayoutPanel中单击的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何知道Click事件中TableLayoutPanel中单击了哪个单元格?



示例:我们只能在TableLayoutPanel上获得按钮的位置,这样就可以获得这样的消息。



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());



答案是(0,1)。



但我想知道单元格的位置而不在面板上添加按钮或其他东西。


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.

推荐答案

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++;
        }
    }


你好,



TableLayoutPanels don'确实有'细胞'这样的,并且真正意味着成为控制的容器。这意味着您无法真正检索单个行,列或单元格。另一种方法是使用面板并在每个面板上放置单独的点击事件。



但是如果您确定要使用TableLayoutPanels,则可以使用从EventArgs在TLP上发生鼠标单击的XY坐标。您可以使用if语句将一系列点定义为其中的行/列。如果你有均匀分布的行/列,你可以用一个循环来做这个。



例如,如果点击发生在X = 40和Column1有范围0-50,0
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>


您可以使用GetColumnWidths和GetRowHeights方法来计算单元格行和列索引:



点? GetRowColIndex(TableLayoutPanel tlp,Point point)

{

if(point.X> tlp.Width || point.Y> tlp.Height)

返回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 - =高度[i];



int row = i + 1;



返回新点(col,row);

}



用法:< br $>


private void tableLayoutPanel1_Click(object sender,EventArgs e)

{

var cellPos = GetRowColIndex(

tableLayoutPanel1,

ta bleLayoutPanel1.PointToClient(Cursor.Position));

}
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));
}


这篇关于如何知道TableLayoutPanel中单击的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆