Checkbox help in C # [closed]

0

I have this code.

private void Verificar()
{
    mUpdater = new DatabaseUpdaterService();

    mUpdater.Initialize(false, null);

    DataTable dt = mUpdater.GetVersionCheckBoxToUpdate();
    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)
{            

}

The purpose is to select the checkbox , I want you to pass the contents of it, so that later I can do the update of the ones I selected. Can anyone help?

    
asked by anonymous 03.05.2017 / 13:01

2 answers

0

Here is the code:

    private void Verificar()
    {
        mUpdater = new DatabaseUpdaterService();

        mUpdater.Initialize(false, null);

        DataTable dt = mUpdater.GetVersionCheckBoxToUpdate();
        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
        }

    }

One observation is that this processing within the event will occur every time the checkbox is checked. Maybe it would be the case if you go through the screen at the time of the Insert / Update and thus get all the values that are checked.

Here is the code, but I think it is more appropriate:

    List<string> versoesSelecionadas;
    private void Verificar()
    {
        mUpdater = new DatabaseUpdaterService();

        mUpdater.Initialize(false, null);

        DataTable dt = mUpdater.GetVersionCheckBoxToUpdate();
        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.Tag = "esseCBfoiCriadoNoMetodoVerificar";
            cb.Location = new System.Drawing.Point(0, h);
            h += cb.Height;
            cb.Show();

        }
    }
    private void LerCheckBox(Control ctrl)
    {
        foreach (Control c in ctrl.Controls)
        {
            if (c is CheckBox)
            {
                string tag = c.Tag == null ? "" : c.Tag.ToString();
                if (tag == "esseCBfoiCriadoNoMetodoVerificar")
                {
                    //Aqui você já tem um objeto CheckBox criado no método verificar
                    if (((CheckBox)c).Checked)
                    {
                        string versao = ((CheckBox)c).Name;
                        //A Versao "versao" está marcada como true
                        versoesSelecionadas.Add(versao);
                    }
                }
            }
            else if (c.HasChildren)
            {
                LerCheckBox(c);
            }
        }
    }

    private void Gravar()
    {
        versoesSelecionadas = new List<string>();

        LerCheckBox(this);

        foreach (string v in versoesSelecionadas)
        {
            MessageBox.Show(v + " foi selecionada");
        }


    }
    
03.05.2017 / 15:15
0

I did not understand your question very well, but from what I understand, you can do this:

void Cb_CheckedChanged(object sender, EventArgs e)
{            
    if (Cb_Checked)
    {
        Verificar();
    }
}
    
03.05.2017 / 14:02