C# winform 动态添加自定义控件

        for (int i = 0; i < 5; i++)
            {
                Control.Refueller r1 = new Control.Refueller();
                r1.sidePanel1.Click += new EventHandler(r1click);
                r1.Name = "jy"+i;
                r1.Size = new Size(220, 281);
                //r1.ResetText = "123123123123";
                //设置上和左位置
                //groubox.Top = 50;
                //groubox.Left = 50;
                //通过坐标设置位置
                r1.Location = new Point(220*i, 0);
                r1.Show();
                panel1.Controls.Add(r1);
                

            }


        private void r1click(object sender, EventArgs e)
        {
            MessageBox.Show(this.Name);

        }

        //添加控件
        public void AddGroupBox()
        {
            string name = "gbox";
            for (int i = 0; i < 3; i++)
            {
                GroupBox gbox = new GroupBox();
                gbox.Name = name + i;
                gbox.Text=name+i;
                gbox.Width = 300;
                gbox.Height = 100;
                gbox.Location = new Point(32, 20 + i * 150);
                this.Controls.Add(gbox);
                //调用添加文本控件的方法
                AddTxt(gbox);
            }
        }
        //添加文本控件
        public void AddTxt(GroupBox gb)
        {
            string name = "txt";
            for (int i = 0; i < 3; i++)
            {
                TextBox txt = new TextBox();
                txt.Name =gb.Name+ name + i;
                txt.Text =gb.Name+"|"+ name + i;
                txt.Location = new Point(12, 15 + i * 30);
                gb.Controls.Add(txt);
            }
        }

找到评论

public void AutoPage()
        {
            int a = 0;
            int pNum = 160 / 10 + 1;
            ViewState["pNum"] = pNum;
            int n = 0;
            for (int i = 0; i < pNum; i++)
            {
                Panel panel = new Panel();
                panel.Visible = false;
                panels[i + 1] = panel;
                for (int j = 0; j < 10; j++)
                {
                    n++;
                    Command cmd = new Command();
                    cmd.Text = "选 择";
                    cmd.BreakAfter = false;
                    panel.Controls.Add(cmd);
                    cmds[n] = cmd;
                    //--------------
                    cmds[n].Click += new EventHandler(cmd_click);
                    cmds[n].CommandName = n.ToString();
                    TextBox txt = new TextBox();
                    txt.Text = a.ToString() + " - This sentence repeats over and over.";
                    //----------
                    cmds[n].Tag = txt;
                    a++;
                    panel.Controls.Add(txt);

                }
                this.Form1.Controls.Add(panel);
                panel.Visible = false;
            }
        }

        void cmd_click(object sender, EventArgs e)
        {
            (((Command)sender).Tag as TextBox).Text = ((Command)sender).CommandName;
        }
发布了134 篇原创文章 · 获赞 13 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_36074218/article/details/103765069