By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,203 Members | 1,401 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,203 IT Pros & Developers. It's quick & easy.

moveing down an item in a listbox - strange behaviour

P: n/a
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


Feb 27 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
"Michael Meckelein" <mi*****@go-on-line.de> wrote in message
news:44***********************@newsread2.arcor-online.net...
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

Feb 27 '06 #2

P: n/a
Hi,
"Stoitcho Goutsev (100)" <10*@100.com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
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 :)
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 27 '06 #3

P: n/a
"Stoitcho Goutsev (100)" wrote
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[i];
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
Feb 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.