Button[] button = new Button[600];
            for (int i = 0; i < 600; i++)
            {
                button[i] = new Button();
                button[i].Width = 50;
                button[i].Text = "按钮"+(i+1).ToString();
                button[i].Left = Left;
                this.Controls.Add(button[i]);
                Left += 50;            }现在按钮只排成一行如何排列成 20列*30行?  按钮在窗体Form1,每行要排20个按钮,如何实现?

解决方案 »

  1.   

    二重循环啊  
     Button[] button = new Button[600];
    for(int a=0;a<30;a++){
                for (int i = 0; i <20; i++)
                {
                    button[i] = new Button();
                    button[i].Width = 50;
                    button[i].Text = "按钮"+(i+1).ToString();
                    button[i].Left = Left;
                    this.Controls.Add(button[i]);
                    Left += 50;            }//要换行代码写在这
    }
      

  2.   

    在form换行代码怎么写呀,就是搞不懂
      

  3.   


    button[i].Location = new Point(100, 120);//设置Button按钮的坐标通过winform面板的大小,和按钮的大小以及index来计算Button的Location实现排列。
      

  4.   


            private void CreateButton()
            {
                int buttonW = 50;//button 宽度
                int buttonH = 24;//button 高度
                int hCount = 3;//每行几个
                int yCount = 2;//多少行
                int hGop = (this.panel1.Width - buttonW * hCount) / (hCount + 1);
                int yGop = (this.panel1.Height - buttonH * yCount) / (yCount + 1);
                for (int j = 0; j < yCount; j++)
                {
                    for (int i = 0; i < hCount; i++)
                    {
                        Button button = new Button();
                        button.Size = new Size(buttonW, buttonH);                    Point p = new Point();
                        p.X = hGop * (i + 1) + i * buttonW;
                        p.Y = yGop * (j + 1) + j * buttonH;
                        button.Location = p;                    button.Text = j + "," + i;
                        this.panel1.Controls.Add(button);
                    }
                }
            }我这里有一个demo大概和你的需求差不多,稍作修改即可。
      

  5.   

                Button[] button = new Button[600];
                 for (int i = 0; i < 600; i++)
                 {
                     button[i] = new Button();
                     button[i].Width = 50;
                     button[i].Text = "按钮"+(i+1).ToString();
                     button[i].Left = Left;
                     button[i].Top = Top;
                     this.Controls.Add(button[i]);
                     Left += 50;
                     if(Left >= 1000)
                     {
                         Left = 0;
                         Top += 20;
                     }
                 }