Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to move items up and down
suppose if selected item 4 at list view and and selected the up button then selected item should move to top and top item should come to 4 th place.

What I have tried:

I'm new to mfc so no idea
so I am reading about this how to implement
Posted
Updated 19-Apr-17 2:27am

1 solution

You have to exchange the items. Get the item contents by index (index 0 and 3 in your example) into variables and set them using the other variable.

How to do this depends on your list style (report or not) and the amount of information stored per item (text, image, check box, user data).

For a report list you have to iterate also over the columns and exchange the text for each column.

See CListCtrl Class[^] for the functions to be used. Candidates are GetItemText and SetItemText for the text (text of columns with report mode), GetItemData and SetItemData for user data, or GetItem and SetItem for multiple settings (image, state, text, format).

Example (assuming the code is part of a CListView derived class):
C++
/* Top item */
int item1 = 0;
/* Selected item (single selections only) */
int item2 = GetListCtrl().GetNextItem(-1, LVNI_SELECTED);
/* Get data for item1 and item2 into variables */
/* Set data */
/* For report style: */
int columns = GetListCtrl().GetHeaderCtrl().GetItemCount();
for (int i = 0; i < columns; i++)
{
    CString str1 = GetListCtrl().GetItemText(item1, i);
    CString str2 = GetListCtrl().GetItemText(item2, i);
    GetListCtrl().SetItemText(item1, i, str2);
    GetListCtrl().SetItemText(item2, i, str1);
}
// Optionally change selection state
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900