Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a list view items, when i click the ADD Button some items adding and that the names shows in the list box, i have two buttons like UP and DOWN, I have some more 50 items names in the list box, default any one items selected in the list box i want to selected items moves up or down (when i click the UP or DOWN Button)

How can i write the coding for this?
Posted
Updated 5-Jul-12 2:21am
v2

When you select a List Item click on Move Down
That time get the Current Index of the Selected List Item
Then remove the item from the Selected Index
And add the same Item object by Insert with Previous Selected Index + 1

For move up, subtract it by 1 i.e. previous selected index - 1

please refer these link

http://stackoverflow.com/questions/4796109/how-to-move-item-in-listbox-up-and-down[^]

http://www.c-sharpcorner.com/uploadfile/dpatra/move-updown-listboxitem-in-listbox-in-wpf/[^]
 
Share this answer
 
v2
Comments
vasanthkumarmk 5-Jul-12 7:21am    
Please give a sample code....
[no name] 5-Jul-12 7:22am    
updated the solution, please check it.
vasanthkumarmk 5-Jul-12 7:24am    
ok i have to check......
[no name] 5-Jul-12 7:32am    
please check and once done mark it as fixed.
vasanthkumarmk 5-Jul-12 7:33am    
I am using List view items that first link shows the list box up & down,
second link shows using WPF. please give any using LIST VIEW ITEMS
C#
private void btnUp_Click(object sender, EventArgs e)
{
    this.checkedListBoxAttribute.ItemCheck -= checkedListBoxAttribute_ItemCheck;
    this.checkedListBoxAttribute.SelectedIndexChanged -= checkedListBoxAttribute_SelectedIndexChanged;
    string item = checkedListBoxAttribute.SelectedItem.ToString();
    int selectIndex = checkedListBoxAttribute.SelectedIndex;
    bool state = checkedListBoxAttribute.GetItemChecked(selectIndex);
    checkedListBoxAttribute.Items.RemoveAt(checkedListBoxAttribute.SelectedIndex);
    checkedListBoxAttribute.Items.Insert(selectIndex - 1, item);
    checkedListBoxAttribute.SetItemChecked(selectIndex - 1, state);
    this.checkedListBoxAttribute.ItemCheck +=checkedListBoxAttribute_ItemCheck;
    this.checkedListBoxAttribute.SelectedIndexChanged+=checkedListBoxAttribute_SelectedIndexChanged;
    checkedListBoxAttribute.SelectedIndex = selectIndex - 1;
}



C#
private void btnDown_Click(object sender, EventArgs e)
{
    this.checkedListBoxAttribute.ItemCheck -= checkedListBoxAttribute_ItemCheck;
    this.checkedListBoxAttribute.SelectedIndexChanged -= checkedListBoxAttribute_SelectedIndexChanged;
    string item = checkedListBoxAttribute.SelectedItem.ToString();
    int selectIndex = checkedListBoxAttribute.SelectedIndex;
    bool state = checkedListBoxAttribute.GetItemChecked(selectIndex);
    checkedListBoxAttribute.Items.RemoveAt(checkedListBoxAttribute.SelectedIndex);
    checkedListBoxAttribute.Items.Insert(selectIndex + 1, item);
    checkedListBoxAttribute.SetItemChecked(selectIndex + 1, state);
    this.checkedListBoxAttribute.ItemCheck += checkedListBoxAttribute_ItemCheck;
    this.checkedListBoxAttribute.SelectedIndexChanged += checkedListBoxAttribute_SelectedIndexChanged;
    checkedListBoxAttribute.SelectedIndex = selectIndex + 1;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900