-1

I have an app in C# that needs two list boxes, but the contents of them are populated according to the state of some checkboxes. I have already figured out how to add content to the list box when the checkbox gets checked, but I want the app to remove that same content if the same checkbox gets unchecked.

NOTE: The Listbox is NOT a checked listbox... it's just a plain listbox. I spent a lot of time looking (in the wrong places, I guess...) and I can't find anything for my specific issue here...

The function I have looks like this:

private void cbCheckbox_CheckedChanged(object sender, EventArgs e)
        {
                if (cbCheckbox.Checked)  
                {
                    testlist.Add("Elemento1");
                    testlist.Add("Elemento2");
                    testlist.Add("Elemento3");
                    ltTestPool.DataSource = testlist;
                }

                else
                {                  
                    testlist.Add("Elemento1");
                    testlist.Add("Elemento2");
                    testlist.Add("Elemento3");
                    ltTestPool.DataSource = testlist;
                }
        }
tshepang
  • 12,111
  • 21
  • 91
  • 136
ElPeta
  • 61
  • 1
  • 9
  • 1
    What exactly is your question? Also the code in your if/else blocks appears to be exactly the same. Is that what you intended? – Abe Miessler Feb 20 '13 at 19:10

2 Answers2

1

Try this code:

private void cbCheckbox_CheckedChanged(object sender, EventArgs e)
        {
                if (cbCheckbox.Checked)  
                {
                    testlist.Items.Clear();
                    testlist.Items.Add("Elemento1");
                    testlist.Items.Add("Elemento2");
                    testlist.Items.Add("Elemento3");
                    ltTestPool.DataSource = testlist;
                }

                else
                {                  
                    testlist.Items.Clear();
                    ltTestPool.DataSource = testlist;
                }
        }
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • Nope... same thing... When I tick the checkbox, it shows the items, but when I untick it, the items are still there.... – ElPeta Feb 20 '13 at 22:58
  • This is very simple code with no mistakes in it. I have also tested it on my pc. May be the problem is in your other code, somewhere else. Kindly recheck your entire code by using `breakpoints`. – Shaharyar Feb 21 '13 at 12:52
0

Clearing your list before setting as datasource should help.

private void cbCheckbox_CheckedChanged(object sender, EventArgs e)
            {
                    if (cbCheckbox.Checked)  
                    {   
                        testlist.Clear();
                        ltTestPool.DataSource = null;
                        testlist.Add("Elemento1");
                        testlist.Add("Elemento2");
                        testlist.Add("Elemento3");
                        ltTestPool.DataSource = testlist;
                    }

                    else
                    {   testlist.Clear();  
                        ltTestPool.DataSource =null;            
                        testlist.Add("Elemento1");
                        testlist.Add("Elemento2");
                        testlist.Add("Elemento3");
                        ltTestPool.DataSource = testlist;
                    }


}
Igoy
  • 2,942
  • 22
  • 23