How to correctly traverse all items in a checkedListBox C #?

1

I have a checkedListBox in a form's tabControl. In the checkedlListBox there are several items that can be selected.

When the user clicks the button below the checkedListBox, a loop is thrown that checks to see if it has selected items or not, if it was, the user is redirected to another tabPage.

If you did not select any option, the MessageBox appears. But the big problem is that this is only for the first item in the checkedListBox.

If you select the second or third and so on, the MessageBox appears as if the user had not selected anything.

So, going back to the title: how to go through all the items in a checkedListBox?

Code that is working with the first item:

    private void btnEditarValores_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= (ListBoxSAdicionais.Items.Count - 1); i++)
        {
            if (ListBoxSAdicionais.GetItemChecked(i))
            {
                this.tabControl1.TabPages.Add(dados3);
                this.tabControl1.SelectTab(2);
                break;
            }
            else if ((ListBoxSAdicionais.GetItemChecked(i) == false))
            {
                MessageBox.Show("Você deve selecionar algum Status Adicional para editar os valores.");
                break;
            }
        }
    
asked by anonymous 17.02.2017 / 17:28

3 answers

1

You can first use the CheckedItems that will return whether or not there are selected items. If the Count is zero, you show the message, otherwise you do the

private void btnEditarValores_Click(object sender, EventArgs e)
{
    if (ListBoxSAdicionais.CheckedItems.Count == 0)
        MessageBox.Show("Você deve selecionar algum Status Adicional para editar os valores.");
    else
    {
        this.tabControl1.TabPages.Add(dados3);
        this.tabControl1.SelectTab(2);
    }            
}
    
17.02.2017 / 17:39
2

The problem is that you are not checking all items because your "for" for the first attempt, because it falls into "if" or "else if";

Suggestion:

bool isChecked = false;

private void btnEditarValores_Click(object sender, EventArgs e)
{
    foreach(CheckBox item in ListBoxSAdicionais.Items)
    {
        if (item.IsChecked == true)
        {
            isChecked = true;

            break;
        }
    }

    if(isChecked == true)
    {
         this.tabControl1.TabPages.Add(dados3);
        this.tabControl1.SelectTab(2);
        break;
    }
    else
    {
        MessageBox.Show("Você deve selecionar algum Status Adicional para editar os valores.");
        break;
    }
}
    
17.02.2017 / 17:37
1

And so my code remained at the end of everything: (It was added else if not to give Unreachable code detected, because if you remove some of the break will be repeating the MessageBox somewhere)

    public void btnEditarValores_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= (ListBoxSAdicionais.Items.Count - 1); i++)
        {
            if (ListBoxSAdicionais.CheckedItems.Count == 0)
            {
                MessageBox.Show("Você deve selecionar algum Status Adicional para editar os valores.");
                break;
            }
            else if (ListBoxSAdicionais.CheckedItems.Count > 0)
            {
                this.tabControl1.TabPages.Add(dados3);
                this.tabControl1.SelectTab(2);
                break;
            }
        }
    
17.02.2017 / 17:54