How to create checkbox dynamically with windows forms?

0

I have a list that returns from the Database and for every record I need to create a% of dynamic form , I did a search and found some examples for WebForms and I need an example for Windows Forms

It would be something like this, Example:

private void CriarCheckboxDinamicamente()
        {
            try
            {
                for (int i = 0; i < 3; i++)
                {
                    this.SuspendLayout();
                    var chk = new System.Windows.Forms.CheckBox();
                    chk.Name = "Motivo" + i.ToString();
                    chk.Text = "Motivo" + i.ToString();
                    pnlModalMotivo.Controls.Add(chk);
                    this.ResumeLayout(false);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Erro: " + ex.Message);
            }
        }

    
asked by anonymous 13.02.2018 / 01:22

1 answer

0

The problem was that the controls were being created overlapping each other, it was necessary to set the Location and it looked like this:

private void btnAdicionar_Click(object sender, EventArgs e)
        {
            try
            {
                int pontoX = 77;
                int pontoY = 86;
                for (int i = 0; i < 3; i++)
                {
                    this.SuspendLayout();
                    var chk = new System.Windows.Forms.CheckBox();
                    chk.Location = new Point(pontoY, pontoX);
                    chk.Name = "chk" + i.ToString();
                    chk.Text = "Motivo" + i.ToString();
                    pnlModalMotivo.Controls.Add(chk);
                    this.ResumeLayout(false);
                    pontoX = pontoX + 50;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Erro: " + ex.Message);
            }
        }
    
13.02.2018 / 13:15