473,395 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

moveing down an item in a listbox - strange behaviour

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
3 1791
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Filiz Duman | last post by:
I was just wondering, is it possible to write into the drop down box in order to jump to a specific item. The reason why I am asking is my drop down box has many items and additionally to the...
2
by: Kyle Blaney | last post by:
I am using a Listbox and can not get the "drop down" effect. My listbox is populated with 20 items and a vertical scrollbar is automatically added. One item is visible. When I click on the...
1
by: JPSutor | last post by:
When I have the user tab over to the listbox control, how can I get one of the selected items to be highlighted without the user manually selecting it. The object is to show a highlighted item...
21
by: Bilal Abbasi | last post by:
I realize that you can add items to a list box as objects so you can have access to more than just one property like the itemindex in vb6. Question I have is how do I cause the listbox to show a...
0
by: Mythran | last post by:
I wrote an editor for a web control that allows it to list all controls on a page in a drop down box in the property grid. In the EditValue method, I have the following (not all the code, but most...
2
by: Alien2_51 | last post by:
I have a ListBox control with the SelectionMode set to MultiExtended bound to an IList collection. The ListBox control is on a tab control, if I have multiple items selected in the list box when I...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
1
by: goldstar | last post by:
Hello All, Just got a quick query, I have an order form, if an item is selected from the ItemID drop down list. this would display within a listbox, and then another item could be ordered. I...
0
by: daonho | last post by:
I've been trying to figure out this problem for so long but no luck. The problem is that I have a ASP.NET Drop Down Listbox with a few item in there, and whenever I select an item from the listbox, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.