moveing down an item in a listbox - strange behaviour

  • Thread starter Thread starter Michael Meckelein
  • Start date Start date
M

Michael Meckelein

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.Text;
lv.Items[selIdx + 1].SubItems.Text =
lv.Items[selIdx].SubItems.Text;
lv.Items[selIdx].SubItems.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.Text and
lv.Items[selIdx].SubItems.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
 
Michael,

Forgive me if I don't understand your problem completely, but I think you
wrote a lot of code for such a simple task.

Try the following code:
private void button1_Click(object sender, EventArgs e)
{
if (this.listView1.SelectedIndices.Count != 1)
return;

int selIndex = this.listView1.SelectedIndices[0];

if (selIndex == this.listView1.Items.Count - 1)
return;

ListViewItem item = this.listView1.SelectedItems[0];
this.listView1.Items.Remove(item);
this.listView1.Items.Insert(selIndex + 1, item);

}

This code makes assumption the the control doesn't support multiple
selection, but it can be easily rewritten to work with multiple selection
too.
 
Hi,


Stoitcho Goutsev (100) said:
Michael,

Forgive me if I don't understand your problem completely, but I think you
wrote a lot of code for such a simple task.

I think the same thing :)
 
Michael,

Forgive me if I don't understand your problem completely, but I think you
wrote a lot of code for such a simple task.

Try the following code:
private void button1_Click(object sender, EventArgs e)
{
if (this.listView1.SelectedIndices.Count != 1)
return;

int selIndex = this.listView1.SelectedIndices[0];

if (selIndex == this.listView1.Items.Count - 1)
return;

ListViewItem item = this.listView1.SelectedItems[0];
this.listView1.Items.Remove(item);
this.listView1.Items.Insert(selIndex + 1, item);

}

This code makes assumption the the control doesn't support multiple
selection, but it can be easily rewritten to work with multiple selection
too.

Hello Stoitcho Goutsev ,

Thanks for you help. You are right. Initial I try it the way you describe
and it did not work. However, I retried it and it works now. Didn't know
what caused the trouble in the past.

Here the code which supports multiple selection.

if (lv.SelectedItems.Count < 1)
return;
// we take the last selected item first and move it down.
for (int i = lv.SelectedItems.Count-1; i >=0; i--)
{
ListViewItem moveItem = lv.SelectedItems;
selIdx = moveItem.Index;
// ignore movedown of last item
if(selIdx==lv.Items.Count-1)
return;

lv.Items.Remove(moveItem);
lv.Items.Insert(selIdx + 1, moveItem);
}

Once again, thx for your help.

Michael
 
Back
Top