c# winform动态生成窗体及控件,并使用控件事件

winform动态生成窗体及控件,并使用控件事件

       private void 提交ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shipei();
        }
        private void shipei()
        {
            win_shipei = new Form();
            GroupBox1 = new GroupBox();

            win_shipei.Name = "winsp";
            win_shipei.Size = new Size(884, 647);
            win_shipei.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            GroupBox1.Text = "注册";
            GroupBox1.Size = new Size(819, 331);
            GroupBox1.Location = new Point(28, 12);

            win_shipei.Show();
            win_shipei.Controls.Add(GroupBox1);
            ArrayList aList = new ArrayList();
            aList.Add("a");
            aList.Add("b");
            aList.Add("c");
            aList.Add("d");
            aList.Add("e");
            aList.Add("f");
            aList.Add("g");
            aList.Add("h");
            for (int i = 1; i < 9; i++)
            {
                Label lb = new Label();
                lb.Name = "Label" + i.ToString();
                lb.Text = (string)aList[i - 1];
                lb.Location = new Point(35, 37 * i);
                lb.Size = new Size(150, 12);
                GroupBox1.Controls.Add(lb);
            }
            for (int i = 1; i < 9; i++)
            {
                tb = new TextBox();
                tb.Name = "textBox" + i.ToString();
                tb.Text = "";
                tb.Location = new Point(200, 37 * i);
                tb.Size = new Size(121, 21);
                GroupBox1.Controls.Add(tb);
            }            
            Button button = new Button();
            button.Name = "button";
            button.Text = "提交";
            button.Location = new Point(723, 349);
            button.Size = new Size(115, 23);
            button.Click += new EventHandler(shipei_Click);
            win_shipei.Controls.Add(button);

            tb = new TextBox();
            tb.Name = "log";
            tb.Location = new Point(28, 378);
            tb.Size = new Size(819, 204);
            tb.Multiline = true;
            tb.BorderStyle = BorderStyle.FixedSingle;
            win_shipei.Controls.Add(tb);
        }
        void shipei_Click(object sender, EventArgs e)
        {
              string textlog = "";
            for (int i = 1; i < 9;i++ )
            {
                TextBox text=win_shipei.Controls.Find("textBox"+i.ToString(), true)[0] as TextBox;
                par.Add (text.Text);
                textlog += text.Text;
            }
            TextBox tblog = win_shipei.Controls.Find("log", true)[0] as TextBox;
            tblog.AppendText(textlog);
        } 

你可能感兴趣的:(c,sharp)