This is a migrated thread and some comments may be shown as answers.

Moving RadListBoxItems from one Listbox to the other

3 Answers 210 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
erwin
Top achievements
Rank 1
Veteran
Iron
erwin asked on 30 Jul 2008, 07:25 AM
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.







3 Answers, 1 is accepted

Sort by
0
Martin Vasilev
Telerik team
answered on 01 Aug 2008, 01:20 PM
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.
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 05 Aug 2008, 11:10 PM
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
0
Martin Vasilev
Telerik team
answered on 07 Aug 2008, 03:09 PM
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.
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
erwin
Top achievements
Rank 1
Veteran
Iron
Answers by
Martin Vasilev
Telerik team
erwin
Top achievements
Rank 1
Veteran
Iron
Share this question
or