Make CheckBox in C # [closed]

0

How to insert CheckBox into the Form, from a database. Where will the table field be the Text of the CheckBox?

Example:

Tabela -- 
0|Versao
1|2.9.15
2|2.9.16

put 2 CheckBox for the user to choose the supported version.

    
asked by anonymous 02.05.2017 / 11:24

1 answer

1

Whereas you have the values you need in a DataTable:

            DataTable dt = new DataTable();
            int h = 0;
            foreach (DataRow r in dt.Rows)
            {
                CheckBox cb = new CheckBox();
                cb.Text = r["nome_do_campo"].ToString();
                cb.Checked = false;
                cb.Parent = this;
                cb.Location = new System.Drawing.Point(0, h);
                h += cb.Height;
                cb.Show();
            }

Implementing the code to use the CheckedChanged event:

 private void MontarCheckBox(DataTable dt)
    {
        int h = 0;
        foreach (DataRow row in dt.Rows)
        {
            CheckBox cb = new CheckBox();
            cb.Text = row["Version"].ToString();
            cb.Name = row["Version"].ToString();
            cb.Checked = false;
            cb.Parent = this; 
            cb.Location = new System.Drawing.Point(0, h); 
            h += cb.Height;
            cb.CheckedChanged += cb_CheckedChanged;
            cb.Show();
        }
    }

    void cb_CheckedChanged(object sender, EventArgs e)
    {
        //sender é o objeto CheckBox onde o evento ocorreu
        CheckBox cb = ((CheckBox)sender);
        if (cb.Checked)
        {
            string versao = cb.Name;
            //Faz o processamento que deseja
        }

    }
    
02.05.2017 / 13:43