我想用以下代码从窗体的右到左依次创建一些picture控件,a的初始值为600,代表该控件离窗体最左边的距离。但我想让这些控件显示在窗体的正中央,并且所创建的控件的个数是不定的。请问这要怎么弄呢?
int a=600; 
for (int i = 0; i<puke.Count; i++)
            {
                pict1[i] = new PictureBox();
                pict1[i].SetBounds(a,350,78, 101);
                pict1[i].BackColor = Color.AliceBlue;
                pict1[i].BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
                this.Controls.Add(this.pict1[i]);
                a -= 23;
                pict1[i].Click += new System.EventHandler(pict1_Click);
            }

解决方案 »

  1.   

    Winfrom和WEB最大的不同就是它有位置的问题(Location = new System.Drawing.Point)
    给你一个例子
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace test
    {
        public partial class CB: Form
        {        [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
               
                Application.Run(new CB());
            }
            public CB()
            {
                int x = 5;
                int y = 26;//位置
                int tbnums = 10;//控件个数,一行数
                int num = 5;
                int j;//控件分行
                int tempi;//和坐标有关的
                int Ygap = 30;//行距
                int Xgap = 80;//列距 LB TB为一列
                for (int i = 0; i < tbnums; i++)
                {
                    j = i / num;//第几行
                    tempi = i % num;
                    Label lb = new Label();
                    lb.Name = "lb" + ( i + 1 ).ToString();
                    lb.Text = ( i + 1 ).ToString() + ".";
                    lb.Size = new System.Drawing.Size(25, 12);
                    lb.Location = new System.Drawing.Point(x + tempi * Xgap, y + j * Ygap);                TextBox tb = new TextBox();
                    tb.Tag = "tb" + i;
                    tb.Name = "tb" + i.ToString();
                    tb.Size = new System.Drawing.Size(50, 20);
                    tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
                    tb.Location = new System.Drawing.Point(x + 25 + tempi * Xgap, y - 9 + j * Ygap);
                    this.Controls.Add(lb);
                    this.Controls.Add(tb);
                }
               Button bt = new Button();
                bt.Text = "第四个的值";
                bt.Click += new EventHandler(button1_Click);
                Button bt1 = new Button();
                bt1.Text = "所有的值";
                
                bt1.Click += new EventHandler(button2_Click);
                bt.Location = new System.Drawing.Point(4, 300);
                bt1.Location = new System.Drawing.Point(84, 300);
                this.Controls.Add(bt);
                this.Controls.Add(bt1);
               
            }
             //分数的文本框只能输入数字
            private void tb_KeyPress(object sender, KeyPressEventArgs e)
            {
                //阻止从键盘输入键
                e.Handled = true;
                if (e.KeyChar >= '0' && e.KeyChar <= '9')
                {
                    e.Handled = false;
                }
                else
                {
                    MessageBox.Show("分数只能是数字!!");
                }        }      private void button1_Click(object sender, EventArgs e)
            {
                //通过ID找
                TextBox tb = (TextBox)this.Controls.Find("tb3", false)[0];
                MessageBox.Show(tb.Text);
            }        private void button2_Click(object sender, EventArgs e)
            {
                //遍历
                string get = "";
                foreach (Control c in this.Controls)
                {
                    if (c is TextBox)
                    {
                        TextBox tb = c as TextBox;
                        get += tb.Text + " ";
                    }
                }
                MessageBox.Show(get.Trim ());        }
        
        }
    }有时候为了方便遍历,可以把它们放在Panel中,这个应该可能解决放在中间的问题,不想加Panel就去找中间的坐标就行了