ListView move Up / Down using Index ??
-
:omg:I have a Form with a ListView and two buttons(Move_Up & Move_Down). I want to reoder the ListView by moving an item up or down by clicking either the button Up / button Down. I get the selected listView item's Index, then use Index + 1 or Index -1 to move the item up / down. The code of move up with Index-1 works. But the code of move down with Index+1 does not work. I don't know why, please help! Thanks! The code is as follows: // this move up works (Index-1) private void btnUp_Click(object sender, System.EventArgs e){ if(listView1.Items.Count != 0){ ListViewItem item = listView1.FocusedItem; if(item != null){ int index = item.Index -1 ListViewItem insert = (ListViewItem) listView1.Items.Insert(index, insert); listView1.Items.Remove(item); } } } // this move down does NOT work (Index +1) private void btnDown_Click(object sender, System.EventArgs e){ if(listView1.Items.Count != 0){ ListViewItem item = listView1.FocusedItem; if(item != null){ int index = item.Index + 1 ListViewItem insert = (ListViewItem) listView1.Items.Insert(index, insert); listView1.Items.Remove(item); } } }
-
:omg:I have a Form with a ListView and two buttons(Move_Up & Move_Down). I want to reoder the ListView by moving an item up or down by clicking either the button Up / button Down. I get the selected listView item's Index, then use Index + 1 or Index -1 to move the item up / down. The code of move up with Index-1 works. But the code of move down with Index+1 does not work. I don't know why, please help! Thanks! The code is as follows: // this move up works (Index-1) private void btnUp_Click(object sender, System.EventArgs e){ if(listView1.Items.Count != 0){ ListViewItem item = listView1.FocusedItem; if(item != null){ int index = item.Index -1 ListViewItem insert = (ListViewItem) listView1.Items.Insert(index, insert); listView1.Items.Remove(item); } } } // this move down does NOT work (Index +1) private void btnDown_Click(object sender, System.EventArgs e){ if(listView1.Items.Count != 0){ ListViewItem item = listView1.FocusedItem; if(item != null){ int index = item.Index + 1 ListViewItem insert = (ListViewItem) listView1.Items.Insert(index, insert); listView1.Items.Remove(item); } } }
It doesn't work becuase.... oh, i'll just show you The list: Item1="1" Item2="2" Item3="3" OK? now lets say you want to move the item at index 2 one step down, your code does two things, add a new item, and removes the old one, right? lest do these two steps- Step 1 - add new item Item1="1" Item2="2" Item3="2" Item4="3" Now if we remove item at index 2+1 (item3 now) what do you think will happen? what you need to do is switchi between the item(index) and item (index+1) the same thing will work with moving an item upwards - switch item(index) with item(index-1) it will be a lot faster then using the insert/b> method, trust me :) Fade (Amit BS)
-
It doesn't work becuase.... oh, i'll just show you The list: Item1="1" Item2="2" Item3="3" OK? now lets say you want to move the item at index 2 one step down, your code does two things, add a new item, and removes the old one, right? lest do these two steps- Step 1 - add new item Item1="1" Item2="2" Item3="2" Item4="3" Now if we remove item at index 2+1 (item3 now) what do you think will happen? what you need to do is switchi between the item(index) and item (index+1) the same thing will work with moving an item upwards - switch item(index) with item(index-1) it will be a lot faster then using the insert/b> method, trust me :) Fade (Amit BS)