Winforms dynamically add controls with code

Lionsure 2020-08-05 Original by the website

Under normal circumstances, Winforms add controls are directly dragged to the Form, this is just one of the ways to add, there is also a flexible way to add, that is, use code to add dynamically. Some controls are not needed when they are initialized. Dragging them to the Form will definitely affect the efficiency of program execution. Especially when there are many such controls, dynamic addition is very necessary.

It should be noted that not every control can dynamically add its sub-controls, such as Label and Button, and it is not possible to drag controls into them in Form. The following are several examples of dynamically adding controls.

 

1. Winforms dynamically add controls for Form

private void Form1_Load(object sender, EventArgs e)
       {
              Panel pa = new Panel();
              pa.Location = new Point(2, 10);

       pa.Width = 200;
              pa.Height = 100;

       pa.BackColor = Color.Beige;//background color
              this.Controls.Add(pa);
       }

 

2. Winforms dynamically add Listview for Panel

private void PanelAddListview()
       {
              Panel pa = new Panel();
              span class="sl">pa.Location = new Point(5, 5);

       pa.Width = 290;
              pa.Height = 290;
              pa.BackColor = Color.Beige;
              this.Controls.Add(pa);

       ListView lv = new ListView();
              lv.Width = 290;
              lv.Height = 280;

       lv.GridLines = true;
              lv.View = View.Details;
              lv.FullRowSelect = true;

       lv.Columns.Add("UserID", 100, HorizontalAlignment.Center);
              lv.Columns.Add("Nickname", 100, HorizontalAlignment.Center);

       ListViewItem lvi = new ListViewItem();
              lvi.SubItems.Clear();
              lvi.SubItems[0].Text = "80095667";
              lvi.SubItems.Add("Robitter");

       lv.Items.Add(lvi);
              pa.Controls.Add(lv);
       }

The running effect diagram is shown in Figure 1:

Winforms dynamically add Listview for Panel

Figure 1

 

 

3. Winforms dynamically add TextBox for Form

private void Form1_Load(object sender, EventArgs e)
       {
              TextBox tb = new TextBox();
              tb.Location = new Point(10, 10);

       tb.Width = 200;
              tb.Height = 22;
              this.Controls.Add(tb);
       }

The same is true for adding Label and Button. If you add TextBox to Panel, just change this to Panel.

 

 

4. Winforms dynamically add PictureBox for Form

private void AddPictureBox()
       {
              PictureBox pb = new PictureBox();
              pb.Name = "Picture name";
              pb.Image = Image.FromFile("E:\\winform\\images\\test.jpg");

        pb.Width = 280;
              pb.Height = 220;
              pb.Location = new Point(10, 10);

       //Add event
              pb.Click += new System.EventHandler(pb_Click);
              this.Controls.Add(pb);
       }

private void pb_Click(object sender, System.EventArgs e)
       {
              PictureBox pb = (PictureBox)sender;
              string pbName = pb.Name;
       }