Checkbox help in C#

Asked

Viewed 83 times

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 goal is, when selecting the checkbox, I want you to pass on the contents of it so that later you can do the update the ones I selected. Anyone can help?

  • 3

    Ana is welcome to Stack Overflow. Your question is a little confused. Could you explain better what you want to do? I recommend reading on tour to understand a little more about the site.

  • 1

    Provide more information so the community can help you.

2 answers

0

I don’t quite understand your question, but from what I understand, you can do this:

void Cb_CheckedChanged(object sender, EventArgs e)
{            
    if (Cb_Checked)
    {
        Verificar();
    }
}
  • This answer is controversial. You say you do not understand, and from what you understand, you present an answer. Maybe the way was to ask for more information.

  • He said he did not understand very well, because the question is not entirely clear, but from what he understood, if it was exactly what he understood this code can help, I do not see "controversial" in this answer. And if that’s exactly what solves the problem?

0


Here’s 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 was the case that you go through the screen at the time of Insert/Update and so get all the values that are checked.

It follows code, more extensive but I think more appropriate to the case:

    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");
        }


    }
  • I’ve done it, thank you all :)

Browser other questions tagged

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