gpt4 book ai didi

c# - 自动在表单上安排无限数量的按钮

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:42:07 33 4
gpt4 key购买 nike

我正在尝试动态生成一堆按钮。按钮的数量取决于有多少坑还活着,这是从数据库中检索的。

这是它的样子:

buttons

这是我用来生成它们的:

private void Form1_Load(object sender, EventArgs e)
{
int numOf = pits.numOfPits();
Button[] genButtons = new Button[numOf];
int l = 1;
for (int i = 0; i < numOf; i++)
{

genButtons[i] = new Button();
genButtons[i].Width = 160;
genButtons[i].Height = 80;
Point location = new Point((i + 1) *200,75);

if (location.X > this.ClientSize.Width - 200)
{
location.X = l * 200;
location.Y += 100;
l++;
}
genButtons[i].Location = location;
this.Controls.Add(genButtons[i]);
}
}

问题是它只适用于 2 行并且有点硬编码。我怎样才能改进它以支持无限数量的按钮。

为了这个问题的目的,你可以忽略表单的有限“高度”。稍后我可能会添加一些分页。

最佳答案

将按钮控件添加到 FlowLayoutPanel .扩展 FlowLayoutPanel 以包括每页项目、当前页面和数据以实现分页。

private void Form1_Load(object sender, EventArgs e)
{
FlowLayoutPanel flp = new FlowLayoutPanel();
for (int i = 0; i < pits.numOfPits(); ++i)
{
Button btn = new Button();
btn.Width = 160;
btn..Height = 80; //set padding or margin to appropriate values
flp.Controls.Add(btn);
}
this.Controls.Add(flp);
}

关于c# - 自动在表单上安排无限数量的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37217450/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com