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

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<std::string>, sort the vector however you want, delete everything from the list box, then copy the vector back in the order you want.

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 LB_SETITEMDATA message? I think it would be much faster.

Edited 3 Years Ago by happygeek: fixed formatting

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.

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?

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

This article has been dead for over six months. Start a new discussion instead.