tags:

views:

1144

answers:

5

Hi,

This is related to my last post.

i want to have up and down buttons on listbox items so that they can moved up/down ie changed its index in the list.

Any ideas how I would actualy do this?

Malcolm

A: 

I am pretty sure you can throw whatever you want into the ListBox. So you could just make your own control that has a label and two buttons for the arrows. Then throw it in the ListBox and attach the events.

Wix
+1  A: 

I would make a button that is an up button, and in its OnClick event, do something like:

int location = listItems.SelectedIndex;
if (location > 0)
{
    object rememberMe = listItems.SelectedItem;
    listItems.Items.RemoveAt(location);
    listItems.Items.Insert(location - 1, rememberMe);
    listItems.SelectedIndex = location - 1;
}

Keep in mind this isn't tested as I don't have visual studio open right now, but it should give you a good idea.

FryGuy
+1  A: 

I would include the order of the item in the model (ie. your data class). Then I'd bind the ListBox to a CollectionView that is sorted by that value. Your up/down buttons would then simply swap the value of this sort property in two of your data items.

HTH, Kent

Kent Boogaart
A: 

Are you binding it? Try to change the order of the item of the binding object and upadate the listbox.

Artur Carvalho
+3  A: 

Use an ObservableCollection as a collection for your ListBox

ObservableCollection has a nifty Move method which fires all the nice events goodness for your listbox to respond...

Arcturus