简体   繁体   English

检查用户在tablelayoutpanel中单击的单元格中是否存在控件

[英]Checking that a control exists or not in a cell where user has clicked in tablelayoutpanel

How can I check that the control exists on that cell where the user has clicked? 我如何检查控件是否存在于用户单击的那个单元格上? Is there any kind of function of the tablelayoutpanel? tablelayoutpanel有什么功能? I just want to check that if the clicked cell HasControl then delete that control in that specific cell. 我只想检查是否单击了单元格HasControl,然后在该特定单元格中删除了该控件。

There isn't a built in function for this, so you would have to find the cell position yourself: 没有内置函数,因此您必须自己找到单元格位置:

void tlp_MouseDown(object sender, MouseEventArgs e) {
  if (e.Button == MouseButtons.Left) {
    int[] colWidths = tlp.GetColumnWidths();
    int[] rowHeights = tlp.GetRowHeights();
    int top = tlp.Parent.PointToScreen(tlp.Location).Y;
    for (int y = 0; y < rowHeights.Length; ++y) {
      int left = tlp.Parent.PointToScreen(tlp.Location).X;
      for (int x = 0; x < colWidths.Length; ++x) {
        if (new Rectangle(left, top, colWidths[x], rowHeights[y])
                          .Contains(MousePosition)) {
          Control c = tlp.GetControlFromPosition(x, y);
          if (c != null) {
            c.Dispose();
          }
        }
        left += colWidths[x];
      }
      top += rowHeights[y];
    }
  }
}

Do note that the functions GetColumnWidths() and GetRowHeights() are not visible from intellisense. 请注意,从智能感知GetRowHeights()函数GetColumnWidths()GetRowHeights() Also, if the user clicks on the control itself, this code won't fire. 另外,如果用户单击控件本身,则不会触发此代码。 You would have to handle the MouseDown event of the control in that cell. 您将必须处理该单元格中控件的MouseDown事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM