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");
}
}
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.
– Marconi
Provide more information so the community can help you.
– Diego Farias