Checkbox in C#

Asked

Viewed 388 times

0

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

Example:

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

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

  • Hello Ana, all right? So, if I got it right you just want the text of the check box to be 2.9.15 or 2.9.16, is that it? I couldn’t quite understand your question. It is possible that you are flagged by it.

  • Hello :) Yes exactly, I wanted the text that appears in the "checkbox text" sent by the database.

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
        }

    }
  • Thank you very much, it worked :)

  • for nothing, =] Dispose.

  • Another question. I wanted now with the previous code executed, imagining that I have 2 checkbox selected. Now I want when these two had selected, to record the value of these chechbox in a variable, and then be able to compare these to other values.

  • in this case, set a name or tag for the checkbox

  • cb. Name = r["column_name"]. Tostring();

  • then you can in some event, trigger the recording of the data, be it button, load, anything.

  • for the checkbox check event, use: cb.Checkedchanged += cb_CheckedChanged;

  • void cb_CheckedChanged(Object Sender, Eventargs e) { //event implementation }

  • I’m gonna put it here like I did, because it’s not working

  • `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(); }

  • private void Cb_checkedchanged(Object Sender, Eventargs e) { Checkbox cb = new Checkbox(); if(cb.Checked) { Start(); } }

  • But error :cb.Checkedchanged += Cb_checkedchanged;

  • is that I will need the name of the selected checkbox to do an update afterwards

  • moment, I will update the answer

  • ready, updated the answer

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.