簡體   English   中英

檢查用戶在tablelayoutpanel中單擊的單元格中是否存在控件

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

我如何檢查控件是否存在於用戶單擊的那個單元格上? tablelayoutpanel有什么功能? 我只想檢查是否單擊了單元格HasControl,然后在該特定單元格中刪除了該控件。

沒有內置函數,因此您必須自己找到單元格位置:

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

請注意,從智能感知GetRowHeights()函數GetColumnWidths()GetRowHeights() 另外,如果用戶單擊控件本身,則不會觸發此代碼。 您將必須處理該單元格中控件的MouseDown事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM