刚学习不久,最近有一个项目,需要在一个panel容器中根据需要自动创建多个TextBox,自动排列成M*N的形式
这个乍弄呀!
各位给指教下
有源码就最好了

解决方案 »

  1.   

    TextBox txt= new TextBox();
    panel.Controls.Add(txt);
    你说的M*N形式不太明白?
      

  2.   

    int m=M;
    int n=N;
    for(int i=0; i<m;i++)
    {
      for(int j=0;j<n;j++)
      {
         TextBox txt= new TextBox();
         panel.Controls.Add(txt);  }
    }
      

  3.   

    for(int i = 0;i<M;i++)
    {
      for(int j = 0;j<N;j++)
      {
        TextBox txt = new TextBox();
        txt.Left = 左上角位置
         txt.Top = 距顶位置
         panel.Control.Add(txt);
      }
    }
      

  4.   

    参考下
    for (...)
                    {
                        // label
                        Label lbl = new Label();
                        lbl.AutoSize = true;
                        lbl.Location = new Point(x, y); //计算每个label的坐标
                        lbl.BackColor = System.Drawing.Color.Transparent;
                        lbl.ForeColor = System.Drawing.SystemColors.HotTrack;
                        lbl.Font = new System.Drawing.Font("SimSun", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                       
                        ++i;
                        this.panel1.Controls.Add(lbl);                 
                    }
      

  5.   


    int cols = 8, rows = 4;TextBox[] tbs = new TextBox[cols*rows];int x = 10, y = 10;for (int i = 0; i<cols*rows;i++)
    {
        if (i % 8 == 0) x = 10;
        // x y 是 location 下班了来不及写了 
    }this.panel1.Controls.AddRange(tbs);
      

  6.   

    TextBox tb = new TextBox();
      tb.Location = ...//根据i,j设置位置
      tb.Name = n.Attributes["Name"].InnerText;
      tb.Text = n.Attributes["Value"].InnerText;
      this.panel1.Controls.Add(tb);
      

  7.   

    补全int cols = 8, rows = 4;TextBox[] tbs = new TextBox[cols * rows];int x = 10, y = 10, spacing = 5;for (int i = 0; i < cols * rows; i++)
    {
        if (i % cols == 0)
        {
            x = 10;
            y = (i == 0) ? 10 : tbs[i - 1].Bottom + spacing;
        }
        tbs[i] = new TextBox();
        tbs[i].Location = new Point(x, y);    x = tbs[i].Right + spacing;
    }this.panel1.Controls.AddRange(tbs);
      

  8.   

    需要设定每个TextBox的宽和高,最左上TextBox的位置,以及上下左右相邻TextBox的间隙。有了这些参数就可以计算第(m,n)个控件的位置。然后循环动态创建一个TextBox,设定Left和Top和其它属性,父容器中Add它就行了。