473,657 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SelectedItem s.Count-1; j >=0; j--)
{
ListViewItem moveItem = lv.SelectedItem s[j];
selIdx = moveItem.Index;
// ignore movedown of last item
if(selIdx==lv.I tems.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 1811
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(o bject sender, EventArgs e)
{
if (this.listView1 .SelectedIndice s.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(it em);
this.listView1. Items.Insert(se lIndex + 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.arco r-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.SelectedItem s.Count-1; j >=0; j--)
{
ListViewItem moveItem = lv.SelectedItem s[j];
selIdx = moveItem.Index;
// ignore movedown of last item
if(selIdx==lv.I tems.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******** ******@TK2MSFTN GP09.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(o bject sender, EventArgs e)
{
if (this.listView1 .SelectedIndice s.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(it em);
this.listView1. Items.Insert(se lIndex + 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.SelectedIte ms.Count < 1)
return;
// we take the last selected item first and move it down.
for (int i = lv.SelectedItem s.Count-1; i >=0; i--)
{
ListViewItem moveItem = lv.SelectedItem s[i];
selIdx = moveItem.Index;
// ignore movedown of last item
if(selIdx==lv.I tems.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
9317
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 scrolling down, it would be good if the user can write into the box in order to get the specific entry the list selected. Is this possible ? Thanks.
2
2092
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 vertical scrollbar I can still only see one item at a time. The effect I want is as follows: The Listbox only displays one item until the user clicks on the right-hand side. When the user clicks on the right-hand side, the list "drops down" to...
1
2332
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 when the control gets focus
21
2255
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 different text after the object has already been added.
0
1031
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 of it): ^snip^ ' Create the listbox to display the control id's. listBox = New ListBox() With listBox .BorderStyle = BorderStyle.None
2
1321
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 tab off the current tab when I come back to the tab with the ListBox only the first item is selected, what could this be...? TIA...Dan
14
18713
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 is that as more and more are added, population of the list box takes longer and longer. ie the first 10th of the item set are added much much quicker than the last 10th. THis occurs with about 40,000 listbox items. My worry is the listbox may...
1
1768
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 want to make a way so that the same item cannot be selected again from the drop down list only for this particular order . So for the next order all the items would reappear and items could be ordered as normal. Any ideas would be much...
0
1074
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 want to display its value in a label. The problem is that this code work properly in both IE and Firefox but not OPERA. It seems like Opera browser doesn't suppose the autopostback attribute of the drop down list. So, it doesn't do anything when I...
0
8407
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.