c#动态添加控件

来源:互联网 发布:mac英文字体打包下载 编辑:程序博客网 时间:2024/06/11 08:52
private void button1_Click(object sender, EventArgs e)
        {
            
            //tabJianCha
            //新建TableLayoutPanel(tab)
            TableLayoutPanel tab = new TableLayoutPanel();
            tab.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            tab.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tab.ColumnCount = 5;
            tab.ColumnStyles.Add(new ColumnStyle(tableLayoutPanel7.ColumnStyles[0].SizeType, tableLayoutPanel7.ColumnStyles[0].Width));
            tab.ColumnStyles.Add(new ColumnStyle(tableLayoutPanel7.ColumnStyles[1].SizeType, tableLayoutPanel7.ColumnStyles[1].Width));
            tab.ColumnStyles.Add(new ColumnStyle(tableLayoutPanel7.ColumnStyles[2].SizeType, tableLayoutPanel7.ColumnStyles[2].Width));
            tab.ColumnStyles.Add(new ColumnStyle(tableLayoutPanel7.ColumnStyles[3].SizeType, tableLayoutPanel7.ColumnStyles[3].Width));
            tab.ColumnStyles.Add(new ColumnStyle(tableLayoutPanel7.ColumnStyles[4].SizeType, tableLayoutPanel7.ColumnStyles[4].Width));
            tab.Left = tableLayoutPanel7.Left;
            tab.Top = tableLayoutPanel7.Top +  tableLayoutPanel7.Height * (panel5.Controls.Count);
            tab.Margin = Padding.Empty;
            tab.RowCount = 1;
            tab.RowStyles.Add(new RowStyle(tableLayoutPanel7.RowStyles[0].SizeType, tableLayoutPanel7.RowStyles[0].Height));
            tab.Size = tableLayoutPanel7.Size;
            tab.TabIndex = tableLayoutPanel7.TabIndex;


            //新建"项目名称"下拉选择框
            ComboBox cb = new ComboBox();
            cb.Anchor = ((AnchorStyles)((AnchorStyles.Left | AnchorStyles.Right)));
            //cb.DropDownAutoSuitWidth = false;
            cb.Location = txtJiaoFeiFangShi.Location;
            cb.Items.AddRange(new object[] {
            "刷卡",
            "现金",
            "银行"});
            cb.Size = txtJiaoFeiFangShi.Size;
            cb.Text = "现金";
            tab.Controls.Add(cb, 0, 0);
            TextBox[] txt = new TextBox[3];


            //新建银行
            txt[0] = new TextBox();
            txt[0].Location = new Point(txtYinHang.Left, txtYinHang.Top);
            txt[0].Anchor = ((AnchorStyles)((AnchorStyles.Left | AnchorStyles.Right)));
            txt[0].Size = new Size(txtYinHang.Width, txtYinHang.Height);
            txt[0].TabIndex = txtYinHang.TabIndex;
            tab.Controls.Add(txt[0], 1, 0);


            //新建卡号
            txt[1] = new TextBox();
            txt[1].Location = new Point(txtKaHao.Left, txtKaHao.Top);
            txt[1].Anchor = ((AnchorStyles)((AnchorStyles.Left | AnchorStyles.Right)));
            txt[1].Size = new Size(txtKaHao.Width, txtKaHao.Height);
            txt[1].TabIndex = txtKaHao.TabIndex;
            tab.Controls.Add(txt[1], 2, 0);




            //新建金额
            txt[2] = new TextBox();
            txt[2].Location = new Point(txtJinE.Left, txtJinE.Top);
            txt[2].Anchor = ((AnchorStyles)((AnchorStyles.Left | AnchorStyles.Right)));
            txt[2].Size = new Size(txtJinE.Width, txtJinE.Height);
            txt[2].TabIndex = txtJinE.TabIndex;
            tab.Controls.Add(txt[2], 3, 0);






            //新建删除按钮
            Button button = new Button();
            button.Anchor = ((AnchorStyles)((AnchorStyles.Left | AnchorStyles.Right)));
            button.Location = new Point(btnAdd.Left, btnAdd.Top);
            button.Size = new Size(btnAdd.Width, btnAdd.Height);
            button.TabIndex = btnAdd.TabIndex;
            button.Text = "删除";
            button.UseVisualStyleBackColor = true;
            tab.Controls.Add(button, 4, 0);
            button.Click += delegate(object sender3, EventArgs e3)
            {
                button.Parent.Dispose();
                int top = panel5.Controls[0].Top;
                for (int i = 0; i < panel5.Controls.Count; i++)
                {
                    panel5.Controls[i].Top = tableLayoutPanel7.Height * i + top;//删除中间一行之后将下方的上移
                }
            };


            panel5.Controls.Add(tab);//把tab添加到panel5
        }