Moving RadListBoxItems from one Listbox to the other

4 posts, 0 answers
  1. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 30 Jul 2008 Link to this post

    2008Q2

    If I move ListBoxItem objects from one Listbox to the other, for example with
    code like that:

    listbox1.Items.AddRange(listBox2.SelectedItems.ToArray())

    and then remove those items from list box 2:

    foreach(RadListBoxItem item in litstbox1.Items)
    {
        if(listbox2.Items.Contains(item))
        {
            listbox2.Items.Remove(item)
        }
    }

    strange things happen. I can no longer select the moved nodes in listbox1 (even though the texts appear).

    I guess a RadListBoxItem cannot be in two listboxes at the same time (but then, an Exception should be thrown).

    I tried to avoid this by using an intermediate List<RadListBoxItem>, but this did not work either.

    Finally ended up with creating code like this:

    foreach(RadListBoxItem in listbox2.SelectedItems)
    {
        listbox1.Add(new RadListBoxItem(item.Text,item.Value))
    }

    which works but IMHO is not very elegant.

    BTW: The forum does not work well with latest versions of Firefox.







  2. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 01 Aug 2008 Link to this post

    Hi Erwin,

    Thank you for writing.

    When moving items between two RadListBoxes first you have to remove the items from list box and after that add them to the second one. This requirement comes because one item can belong only to one ItemsCollection. Please, review the code-block below as example:
    RadListBoxItemCollection itemsToMove = new RadListBoxItemCollection(); 
    foreach (RadListBoxItem item in this.radListBox3.SelectedItems) 
        itemsToMove.Add(item); 
     
    foreach (RadListBoxItem item in itemsToMove) 
        this.radListBox3.Items.Remove(item); 
        this.radListBox2.Items.Add(item); 

    I hope this helps. If you have other questions, do not hesitate to contact me again.

    Sincerely yours,
    Martin Vasilev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 05 Aug 2008 Link to this post

    if you do the add/remove in opposite sequence, you end up with unselectable items in the destination list box.

    if a listboxitem cannot be in 2 or more listboxes at the same time, it should IMHO throw an exception in case, the programmer does it the wrong way. I have found no reference in the documentation about that limitation.

    regards
    erwin
  5. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 07 Aug 2008 Link to this post

    Hello erwin,

    Thank you for your feedback.

    We highly appreciate all customer notes and we will consider adding demo for moving ListBoxItems scenario.

    If you need additional assistance, do not hesitate to contact me again.

    Kind regards,
    Martin Vasilev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top