c# 根据 mySQL Workbench 中的项目数添加新按钮

c# adding New Button Depending on the # of items in mySQLWorkbench

此代码根据保存在我的项目中的项目数量添加新按钮 table。

mySQL WorkBench

我不知道为什么它一直在复制我的按钮。我只想将按钮排列成 4 列。

    object[] itemDetail;
    object[] itemLi = itemsWS.searchItem("", "drinks", "all");
    int cleft = 0;
    for (int i = 0; i < itemLi.Length; i++)
    {


        itemDetail = itemsWS.getItemInfo(itemLi[i].ToString());


        for (int x = 35; x < 537; x++)
        {
            Button myButton = new Button();
            myButton.Text = itemDetail[0].ToString();
            myButton.Top = cleft * 80;
            myButton.Left = 70;
            myButton.Location = new Point(x, cleft);
            myButton.Size = new Size(100, 60);
            tabPage1.Controls.Add(myButton);


            cleft = cleft + 15;
            //cleft = cleft + 1;

            x += 134;
        }

    }

尝试这样的事情:

int x = 35;
int cleft = 0;

foreach (var item in itemLi)
{
    Button myButton = new Button();
    myButton.Text = itemDetail[0].ToString();
    myButton.Top = cleft * 80;
    myButton.Left = 70;
    myButton.Location = new Point(x, cleft);
    myButton.Size = new Size(100, 60);
    tabPage1.Controls.Add(myButton);

    x += 134;
    // Check if x is greater than form size,
    // If so, resets x, and increments cleft
    if (x >= 537)
    {
        x == 35;
        cleft += 15
    }
}

我不太确定 xcleft 在做什么,但你明白了(希望如此)

你也可以把FlowLayoutPanel放在Bread标签页里面,然后只设置FlowLayoutPanelOrientation 属性,然后不做Ben的建议TopLeftLocationcleftx 这样您就不需要设置按钮的位置了。