[英]How to swap elements on TableLayoutPanel?
我在以編程方式交換TableLayoutPanel
上的兩個元素時遇到麻煩。 是否有可能在運行時交換TableLayoutPanel上駐留的控件,就像我們可以在Form
設計時做到這一點一樣? 我在窗體上有25個按鈕,每個TableLayoutPanel
單元格一個按鈕。
public void Bind(Cell tile1,Cell tile2)
{
tableLayoutPanel1.SetRow(this.Controls.Find(tile1.Name,true).FirstOrDefault() as Button, tile2.Row);
tableLayoutPanel1.SetColumn(this.Controls.Find(tile1.Name, true).FirstOrDefault() as Button, tile2.Column);
tableLayoutPanel1.SetRow(this.Controls.Find(tile2.Name, true).FirstOrDefault() as Button, tile1.Row);
tableLayoutPanel1.SetColumn(this.Controls.Find(tile2.Name, true).FirstOrDefault() as Button, tile1.Column);
}
使用GetCellPosition和SetCellPosition:
private void SwapCells(Cell tile1, Cell tile2) {
Control c1 = tlp.GetControlFromPosition(tile1.Column, tile1.Row);
Control c2 = tlp.GetControlFromPosition(tile2.Column, tile2.Row);
TableLayoutPanelCellPosition cell1 = tlp.GetCellPosition(c1);
tlp.SetCellPosition(c1, tlp.GetCellPosition(c2));
tlp.SetCellPosition(c2, cell1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.