Question

When I check the checkbox in the checkboxlist, the checkbox.text will add into the listbox. But when I uncheck the checkbox, the checkbox.text will be removed from the listbox. But the problem is I do not know how to remove the selected items from the listbox.

[Press here to see the picture][2]<br>

For example, when i check the checkbox1, checkbox2, checkbox3, the listbox will display
checkbox1 checkbox2 checkbox3

However, when i uncheck the checkbox, Still Same I stuck at here. Help !!

here is my code:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked) listBox1.Items.Add(checkBox1.Text);

}

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox2.Checked) listBox1.Items.Add(checkBox2.Text);
}

private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox3.Checked) listBox1.Items.Add(checkBox3.Text);
}

private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox4.Checked) listBox1.Items.Add(checkBox4.Text);
}
Was it helpful?

Solution 4

You can do one funciton

 private void checkBoxCheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if (cb != null)
                if (cb.Checked) listBox1.Items.Add(cb.Text); else listBox1.Items.Remove(cb.Text);
        }

And then add it for all your checkboxes as CheckedChanged event.

OTHER TIPS

if (checkBox3.Checked) 
    listBox1.Items.Add(checkBox3.Text);
else
    listBox1.Items.Remove(checkBox3.Text);

Note that this will always remove whatever is in the Text property. This means that, if I check the box, change the text in textBoxX, and then uncheck, it will remove a different item.

The Items collection on a ListBox has a Remove method. Put an else in each of your CheckedChanged events and use the Remove method.

if (checkBox4.Checked) listBox1.Items.Add(checkBox4.Text);
else listBox1.Items.Remove(checkBox4.Text);

Make a common function and call. For e.g.

private void addRemove(CheckBox chk)
{
if (chk.Checked) 
    listBox1.Items.Add(chk.Text);
else
    listBox1.Items.Remove(chk.Text);
}

Call

addRemove(checkbox1); 

Call Remove() method. It accepts one argument that specifies the item to remove.

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{

    if (checkBox1.Checked) 
        listBox1.Items.Add(checkBox1.Text);

    else
        listBox1.Items.Remove(checkBox1.Text);

}

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{

    if (checkBox2.Checked) 
        listBox1.Items.Add(checkBox2.Text);

    else
        listBox1.Items.Remove(checkBox2.Text);

}

private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox3.Checked) 
        listBox1.Items.Add(checkBox3.Text);

    else
        listBox1.Items.Remove(checkBox3.Text);

}

private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
   if (checkBox4.Checked) 
      listBox1.Items.Add(checkBox4.Text);

   else
      listBox1.Items.Remove(checkBox4.Text);

}

If thats all you want to do why not assign the same event to every checkbox and do the below:-

CheckBox chkBox=(CheckBox)sender;
if (chkBox.Checked) 
    listBox1.Items.Add(chkBox.Text);
else
    listBox1.Items.Remove(chkBox.Text);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top