How to correctly scroll through all items in a checkedListBox C#?

Asked

Viewed 684 times

1

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

When the user clicks on the button below the checkedListBox, a loop is launched that checks whether or not they have selected items, if any, the user is redirected to another tabPage.

If you have not selected an option, Messagebox appears. But the big problem is that only for the first checkedListBox item.

If the second or third is selected and etc, Messagebox appears as if the user had not selected anything.

Then, returning to the title: how to go through all the items of 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;
            }
        }
  • wouldn’t be the break inside if the problem?

  • When the user is redirected to tabPage(data3) the Messagebox of the other if should not appear and if I take the break from the first if, it keeps appearing. If I break Else if, you’ll be sending Messagebox until the loop goes through the amount of items in the checkedListBox. I’ve tried to remove the alternating breaks, it doesn’t work.

3 answers

2

The problem is that you are not checking all the items because your "for" stops on the first try as it falls into the "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;
    }
}
  • 1

    I understood the code and even I will use it for something else, because this current problem already solved. Thank you very much for the help!

  • Just to be clear, my comment was for question and not for your answer (which is right). I got confused and commented in the wrong place. And though this isChecked == true Bother me, it wasn’t me who denied =D

  • I agree, Pablovargas' response was better. Even though they had not been negative. There are always some downvotes roaming the site. hahaha But tell me about isChecked, I will improve my logic.

1


You can first use the method CheckedItems which will return whether or not there are selected items. If the Count be zero, you show the message, otherwise you make the tab change

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);
    }            
}
  • That was it... Well, how come I didn’t think of it? haha. C# is my first programming language and I’m trying to make the famous "Learn by Doing". Thank you very much ;)

1

And so was my code 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;
            }
        }
  • Wevelly Felipe, you don’t need the for and you don’t need the break.

  • I edited my reply with the button click event

  • Another detail, you don’t need to answer by showing how your code looks, just accept my answer as correct.

  • Ah ok, thanks again ^^.

Browser other questions tagged

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