![](/img/trans.png)
[英]What is the best way to save a matrix of Checkboxes when x/y are not known until runtime?
[英]Create checkboxes at runtime and fit them on form on the best possible way
我需要在 Winform 上創建 N 個復選框,但隨后我需要根據表單大小以最佳方式將它們放入表單中。
這是代碼
int x = 0;
foreach (int i in Enumerable.Range(0, 5))
{
foreach (int j in Enumerable.Range(0, 4))
{
CheckBox b = new CheckBox();
b.Appearance = Appearance.Button;
b.Size = new System.Drawing.Size(80, 30);
b.Location = new Point(i * 80, j * 30);
b.Text = x.ToString();
b.CheckedChanged += B_CheckedChanged;
b.Tag = x.ToString("#00");
this.Controls.Add(b);
x++;
}
}
這段代碼在 5 列和 4 行中創建了 20 個復選框,但我現在需要在用戶輸入 34 時以某種方式找到顯示這些復選框的最佳方式。 假設在 5 行中,每行包含 8 個復選框,但最后一行包含最后 2 個復選框。 實現這一目標的最佳方法是什么?
您可以使用將所有 chekbox 放在 flowLayoutPanel 控件中,設置 flowlayoutpanel 控件 Anchor,以便在表單大小更改時 flowlayoutpanel 可以自動更改大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.