Hello,
I run into trouble move down a selected item in a listbox. The code moving
down the item is the following one:
for (int j = lv.SelectedItems.Count-1; j >=0; j--)
{
ListViewItem moveItem = lv.SelectedItems[j];
selIdx = moveItem.Index;
// ignore movedown of last item
if(selIdx==lv.Items.Count-1)
return false;
// move the subitems for the next row
// cache so we can move the selected row down
for(int i=0; i < lv.Items[selIdx].SubItems.Count; i++)
{
cache = lv.Items[selIdx + 1].SubItems[i].Text;
lv.Items[selIdx + 1].SubItems[i].Text =
lv.Items[selIdx].SubItems[i].Text;
lv.Items[selIdx].SubItems[i].Text = cache;
}
lv.Items[selIdx+1].Selected=true;
lv.Items[selIdx].Selected = false;
}
Move a selected item with this code, the item lost all its information (the
information stored in the SubItems). It's strange. The debugger shows the
caching works, lv.Items[selIdx + 1].SubItems[i].Text and
lv.Items[selIdx].SubItems[i].Text have the expected data stored until the
step lv.Items[selIdx+1].Selected is executed. Then the moved item shows the
default values.
It is strange, isn't it? I found out if I change the order of the selection
/ deselection of the item,
lv.Items[selIdx].Selected = false;
lv.Items[selIdx+1].Selected=true;
the code works as expected.
I have no explaination for that. Can anyone explain me the reason,
lv.Items[selIdx+1].Selected=true;
lv.Items[selIdx].Selected = false;
does not work, but
lv.Items[selIdx].Selected = false;
lv.Items[selIdx+1].Selected=true;
works fine?
Thanks in advance.
Michael