Select in windows form c#

Asked

Viewed 269 times

3

Imagining I have 3 checkbox:

  • 2.9.1
  • 2.9.2
  • 2.9.3

I wanted, that I click on the 2.9.3, selects me all that are behind. It’s the kind of a select all, but only select me from a given number upwards that I choose. I’m doing this checkbox, inside a listview, and I want to select as I said earlier.

Updating

private void Checked()
{
    foreach (UltraListViewItem listItem in listView.Items)
    {
        if (cb_selectAll.Checked == true)
        {
            listItem.CheckState = CheckState.Checked;
        }
        if (cb_selectAll.Checked == false)
        {
            listItem.CheckState = CheckState.Unchecked;
        }
    }
}
  • Share the code you’ve tried to make.

  • I made the code for select all :

  • I think there is confusion in the use of the site. You completed your question in one answer... Maybe read on FAQ of Sopt help you.

  • Solved your problem Ana ?

  • I haven’t done that part yet.

  • I have solved my problem. Thank you very much ;)

Show 1 more comment

2 answers

1

The method below will create a list of all checkbox of the previous versions, and finally, we will assign this method to the event of each checkbox.

private void SelecionaCheckVersoesAnteriores(object sender, EventArgs e) {
    //Esse é o checkbox que você vai clicar (dar o check)
    var checkAtivado = (CheckBox)sender;

    //Cria uma lista com todos os checkbox com um nome MENOR que o clicado
    //O texto de cada checkbox foi convertido para número para utilizarmos o operador <
    var listCheck = this.Controls?.OfType<CheckBox>().Where(p => Convert.ToInt32(p.Text.Replace(".", ""))
                                                                 < Convert.ToInt32(checkAtivado.Text.Replace(".", "")));
    //Percorre todos os itens da lista e atribui o mesmo valor:
    //Se você dar check, então todos os anteriores vão ser checados, senão,
    //todos serão desmarcados
    foreach (var item in listCheck) {
        item.Checked = checkAtivado.Checked;
    }

    //OU Se quiser que apenas aconteça ao dar um check, ou seja, só quando ativar,
    //então marque como true diretamente
    foreach (var item in listCheck) {
        item.Checked = true;
    }
}

//Aqui estão as atribuições ao evento Check de cada checkbox
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
    SelecionaCheckVersoesAnteriores(sender, e);
}
private void checkBox2_CheckedChanged(object sender, EventArgs e) {
    SelecionaCheckVersoesAnteriores(sender, e);
}
private void checkBox3_CheckedChanged(object sender, EventArgs e) {
    SelecionaCheckVersoesAnteriores(sender, e);
}

1


You can create an event so that when marking an item it marks the previous items;

 private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (e.CurrentValue != CheckState.Checked)
        {
            for (int i = e.Index - 1; i >= 0 ; i--)
            {
                checkedListBox1.SetItemChecked(i, true);
            }
        }
    }
  • Devexpress.XtraEditors.Controls.Itemcheckeventargs e) , this does not recognize me in c#

  • It is because this example is using Devexpress components, but Voce can use windows Forms components : System.Windows.Forms.Itemcheckeventargs

  • Look Itemcheckeventargs doesn’t have the e.State.

  • private void listView_ItemCheckStateChanged(Object Sender, Itemcheckstatechangedeventargs e) { if() { for } }. I have this, and now what do I put? Gives me nothing

  • I edited the resolution, in the case I’m using a checkedListBox;

Browser other questions tagged

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