954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to move a selected item up/down in List Box?

Hi guys:

I am using Borland C++ Builder to make a window application. I need to use a list box to display the items I selected. Besides, I need to chang the order of the selected items in the box. Could any body please give me a clue how I can do this?

Thanks in advance!

Doug

driplet
Light Poster
28 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

How about looking at the facilities offered by the API?

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

I don't think CListBox has a method to move a row from one spot to another. If you want to change the order of the items then I'd probably copy them all into a std::vector, sort the vector however you want, delete everything from the list box, then copy the vector back in the order you want.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Yeah. There is no such function in the API too. But I don't know if the list box will flicker if you remove and add the items all at once. Can't you do it by using the <a href="http://msdn2.microsoft.com/en-us/library/ms671439.aspx">LB_SETITEMDATA</a> message? I think it would be much faster.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Why would he want to use LB_SETITEMDATA ? That will not change the order of the items that appear in the list box.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I don't know. It was just a hunch. I am too lazy to try it and see. Another straightforward way seems to be the use of LB_INSERTSTRING . Since you can specify the index of the position that you want to insert the string to, I think you can use it to change the order of the items.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Yes, LB_INSERTSTRING will work if all he wants to do is insert the new item in a specific location.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Yeah. But won't deleting an item from it's original location and inserting it in it's new location change the order of the items?

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Changing the order of the items is what the OP wants to do.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This snippet sends the string at zero based index 3, to the position at index 0.

HWND hList = GetDlgItem(hwnd, IDC_LIST);
char buf[100]="";
SendMessage(hList, LB_GETTEXT, (WPARAM)3, (LPARAM)buf);
SendMessage(hList, LB_DELETESTRING, 3, 0);
SendMessage(hList, LB_INSERTSTRING, 0, (WPARAM)buf);

The original order of 0 1 2 3 4 5 will endup with 3 0 1 2 4 5

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You