C# 动态操作winform控件算法

  private void newusers(string username, string password, string rols)
        {
            int j = 0;
            TextBox txt = new TextBox();//新建textbox控件对象
            txt.Width = 80;
            Point loc = new Point();
            loc.X = 20 + j * 100;
            j++;
            loc.Y = 80 + 40 * line;
            txt.Location = loc;
            txt.Text = username;
            txt.Name = "tb" + line.ToString() + j.ToString();
            if (line == 0)
            {
                txt.Enabled = false;
            }
            this.Controls.Add(txt);//form中添加新建的控件对象
            TextBox txt2 = new TextBox();
            txt.Width = 80;
            loc.X = 12 + j * 100;
            j++;
            loc.Y = 80 + 40 * line;
            txt2.Location = loc;
            txt2.Text = password;
            txt2.Name = "tb" + line.ToString() + j.ToString();
            if (line == 0)
            {
                txt2.Enabled = false;
            }
            this.Controls.Add(txt2);
            ComboBox cb = new ComboBox();//新建combox对象
            cb.Items.Add("管理员");
            cb.Items.Add("审核员");
            cb.Items.Add("记账员");
            if (rols == "1")
            {
                cb.SelectedIndex = 0;
            }
            else if (rols == "2")
            {
                cb.SelectedIndex = 1;
            }
            else if (rols == "3")
            {
                cb.SelectedIndex = 2;
            }
            cb.Width = 80;
            loc.X = 40 + j * 100;
            j++;
            loc.Y = 80 + 40 * line;
            cb.Location = loc;
            cb.Name = "combox" + line.ToString();
            if (line == 0)
            {
                cb.Enabled = false;
            }
   
                this.Controls.Add(cb);//form中加入combox 对象
                if (line >= 1)
                {
                Button bt = new Button();//新建button对象
                loc.X = 40 + j * 100;
                j++;
                loc.Y = 80 + 40 * line;
                bt.Location = loc;
                bt.Name = "button" + line.ToString();
                bt.Text = "del";
                bt.Click += new EventHandler(btnSubmit_Click);
                this.Controls.Add(bt);//加入button对象
            }
                line++;
        }

 

 private void deleteline(string i)
        {
            int j = Int32.Parse(i);
            int init = j;
            if (j > line - 1)
            {
                MessageBox.Show("溢出错误");
            }
            else
            {
                for (; j < line;j++ )
                {
                    if (j == line - 1)
                    {
                        string tb1 = "tb" + j.ToString()+"1";
                        string tb2 = "tb" + j.ToString()+"2";
                        string cb = "combox" + j.ToString();
                        string bt = "button" + j.ToString();
                        foreach (Control t in this.Controls)//遍历form中的控件
                        {
                            TextBox tb = t as TextBox;//强制赋予对象类型TextBox
                            if (tb != null)//若赋值成功
                            {
                                if (tb.Name == tb1)//判定是否是所需Textbox
                                {
                                    this.Controls.Remove(t);//删除操作此控件
                                }
                            }
                        }
                        foreach (Control t in this.Controls)
                        {
                            TextBox tb = t as TextBox;
                            if (tb != null)
                            {
                                if (tb.Name == tb2)
                                {
                                    this.Controls.Remove(t);
                                }
                            }
                        }
                        foreach (Control c in this.Controls)
                        {
                            ComboBox cbx = c as ComboBox;
                            if (cbx != null)
                            {
                                if (cbx.Name == cb)
                                {
                                    this.Controls.Remove(c);
                                    break;
                                }
                            }
                        }
                        foreach (Control b in this.Controls)
                        {
                            Button btn = b as Button;
                            if (btn != null)
                            {
                                if (btn.Name == bt)
                                {
                                    this.Controls.Remove(b);
                                    break;
                                }
                            }
                        }
                        line--;
                    }
                    else
                    {
                        string firtb1 = "tb" + j.ToString() + "1";
                        string firtb2 = "tb" + j.ToString() + "2";
                        string fircb = "combox" + j.ToString();
                        string sentb1 = "tb" + (j + 1).ToString() + "1";
                        string sentb2 = "tb" + (j + 1).ToString() + "2";
                        string sencb = "combox" + (j + 1).ToString();
                        string t1 = null;
                        string t2 = null;
                        string comb = null;
                        foreach (Control t in this.Controls)
                        {
                            TextBox tb = t as TextBox;
                            if (tb != null)
                            {
                                if (t.Name == sentb1)
                                {
                                    t1 = t.Text;
                                }
                                if (t.Name == sentb2)
                                {
                                    t2 = t.Text;
                                }
                            }
                            ComboBox cbcc = t as ComboBox;
                            if(cbcc!=null)
                            {
                                if (t.Name == sencb)
                                {
                                    comb = t.Text;
                                }
                            }
                        }
                        foreach (Control t in this.Controls)
                        {
                            TextBox tb = t as TextBox;
                            if (tb != null)
                            {
                                if (t.Name == firtb1)
                                {
                                    t.Text = t1;
                                }
                                if (t.Name == firtb2)
                                {
                                    t.Text = t2;
                                }
                            }
                            ComboBox cbbox = t as ComboBox;
                            if (cbbox != null)
                            {
                                if (t.Name == fircb)
                                {
                                    t.Text = comb;
                                }
                            }
                        }
                    }
                }
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值