Winforms creation manages dynamically

I am learning C #. I want to create some controls dynamically. Here is the code I'm trying to create new elements in the form dynamically, but it does nothing. Please help me solve this problem.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

    namespace Sampless
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int n = 4;

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            TextBox[] textBox = new TextBox[n];
            Label[] label = new Label[n];

            for (int i = 0; i < n; i++)
            {
                textBox[i] = new TextBox();
                textBox[i].Name = "n" + i;
                textBox[i].Text = "n" + i;

                label[i] = new Label();
                label[i].Name = "n" + i;
                label[i].Text = "n" + i;
            }

            for (int i = 0; i < n; i++)
            {
                this.Controls.Add(textBox[i]);
                this.Controls.Add(label[i]);
            }
        }
    }
}
+4
source share
6 answers

You add all the controls on top of each other, so it looks like there is only one of them. You want to put them in some kind of control / control panel based on the layout (for example FlowLayoutPanel), which will automatically place the structures in the appropriate place depending on the type of layout you want.

+7
source

# . .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        TextBox txtBox;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtBox = new TextBox();
            txtBox.Location = new Point(10, 50);
            txtBox.Visible = true;
            Controls.Add(txtBox);
        }
    }
}

------------------------------------------ ---- ---------------------------------

4 4 , . ( ), .

P.s.: .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        TextBox[] txtBox;
        Label[] lbl;

        int n = 4;
        int space = 20;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtBox = new TextBox[n];
            lbl = new Label[n];

            for (int i = 0; i < n; i++)
            {
                txtBox[i] = new TextBox();
                txtBox[i].Name = "n" + i;
                txtBox[i].Text = "n" + i;

                lbl[i] = new Label();
                lbl[i].Name = "n" + i;
                lbl[i].Text = "n" + i;
            }


            for (int i = 0; i < n; i++)
            {
                txtBox[i].Visible = true;
                lbl[i].Visible = true;
                txtBox[i].Location = new Point(40, 50 + space);
                lbl[i].Location = new Point(10, 50 + space);
                this.Controls.Add(txtBox[i]);
                this.Controls.Add(lbl[i]);
                space += 50;
            }

        }
    }
}

: http://shrani.si/f/1F/Y/22BgTuBX/example.png

, Sendspace.

: http://www.sendspace.com/file/glc7h2

+1

, .

:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

    namespace Sampless
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int n = 4;

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            TextBox[] textBox = new TextBox[n];
            Label[] label = new Label[n];
            int labelX, labelY, textboxX, textboxY;

            labelX = 20;
            labelY = 20;
            textboxX = 50;
            textboxY = 20;

            for (int i = 0; i < n; i++)
            {
                textBox[i] = new TextBox();
                textBox[i].Name = "n" + i;
                textBox[i].Text = "n" + i;
                textBox[i].Location = new Point(textboxX, textboxY);

                label[i] = new Label();
                label[i].Name = "n" + i;
                label[i].Text = "n" + i;
                label[i].Location = new Point(labelX, labelY);

                labelY += 25;
                textboxY += 25;
            }

            for (int i = 0; i < n; i++)
            {
                this.Controls.Add(textBox[i]);
                this.Controls.Add(label[i]);
            }
        }
     }
}

2 , 25 Y .

+1

. .

TextBox[] txtTeamNames = new TextBox[teams];
for (int i = 0; i < txtTeamNames.Length; i++) {
  var txt = new TextBox();
  txtTeamNames[i] = txt;
  txt.Name = name;
  txt.Text = name;
  txt.Location = new Point(172, 32 + (i * 28));
  txt.Visible = true;
}
0

, , . , - pnl1,

pnl1.Controls.Add(textBox[i]);
pnl1.Controls.Add(label[i]);
0
namespace Sample
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        TextBox txbx = new TextBox();
        private void button1_Click(object sender, EventArgs e)
        {
            AddNewTextBox();

            txbx = new TextBox();

            txbx.Location = new Point(10, 20);

            txbx.Visible = true;

            Controls.Add(txbx);

        }

    }
}
0

Source: https://habr.com/ru/post/1525829/


All Articles