checkedlistbox

c#: how uncheck the previous box when a new one is checked

selectionMode is one and i have only one selected but more than one checked. CheckedItems is not updatable. I think i have to use ItemCheck event.

Thx




Answer this question

checkedlistbox

  • smozaffari

     manuel0081 wrote:
    The situation is that i have a checked box. When i check another box, i want uncheck all the other...why is it not default behaviour

    Because then it would be a RadioButtonListBox or are OptionButtonListBox

    But as far as i can see the SetItemChecked method is support by Framework version 1.0, 1.1 and 2.0.

    See the documentation here.



  • xSephirothx

    Just walktrough all items and uncheck them. You can optimize this by caching the last checked item and only unset that one.


    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
    if( e.NewValue != CheckState.Checked )
    {
    return;
    }

    for( int i = 0; i < checkedListBox1.Items.Count; i++ )
    {
    if( i != e.Index )
    {
    checkedListBox1.SetItemChecked( i, false );
    }
    }
    }




  • vbrook

    Where is .SetItemChecked in VS 2005

    The situation is that i have a checked box. When i check another box, i want uncheck all the other...why is it not default behaviour



  • ichi

    Here is the other example with a cache of checked items:


    private ArrayList _checkedItems = new ArrayList();

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
    if( e.NewValue != CheckState.Checked )
    {
    return;
    }
    else
    {
    for( int i = _checkedItems.Count -1; i >= 0 ; i-- )
    {
    object item = _checkedItems[ i ];
    int index = checkedListBox1.Items.IndexOf( item );

    checkedListBox1.SetItemChecked( index, false );
    _checkedItems.RemoveAt( i );
    }

    object currentItem = checkedListBox1.Items[ e.Index ];
    _checkedItems.Add( currentItem );
    }
    }




  • checkedlistbox