Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to move selected row in clistcontrol up and down without deleting and make sure that it will gove down upt olast while selecting button ..please let me know.

What I have tried:

LVITEM lvi;
Posted
Updated 23-Mar-17 4:35am
v2

Hi,
For previous version of MFC, I used the functions SetSel and SetTopndex.
Show help for this functions.
There are also many articlehere about CListCtrl.
Best regards
 
Share this answer
 
Comments
Member 12677926 23-Mar-17 4:49am    
could you pleas help with logic
It is not possible to do this without deleting. You have to create a copy of the row at the new loaction and delete the initial row.

Searching the web for "clistctrl move item up down" gives you examples:
Copying/Moving Rows in CListCtrl[^]
c++ - Move an item up or down in a list box - Stack Overflow[^]

[EDIT]
To move items one step it would be possible to exchange them:

  • Get item at selected index and store data in variable
  • Get item at new position (index +/- 1) and store data in variable
  • Set item at selected position with data from new position
  • Set item at new position with data from selected position
  • Change selection to new position

Untested example:
C++
// Single row must be selected
if (1 != GetSelectedCount())
    return;
int sel = GetNextItem(-1, LVNI_SELECTED); 
// Move up or down
int next = bUp ? sel - 1 : sel + 1;
if (next < 0 || next >= GetItemCount())
    return;
CString strSel, strNext;
for (int i = 0; i < GetHeaderCtrl()->GetItemCount(); i++)
{
    strSel = GetItemText(sel, i);
    strNext = GetItemText(next, i);
    SetItemText(sel, i, strNext);
    SetItemText(next, i, strSel);
}
// Move selection
SetItemState(sel, ~LVNI_SELECTED, LVIS_SELECTED);
SetItemState(next, LVNI_SELECTED, LVIS_SELECTED);
SetSelectionMark(next);
[/EDIT]
 
Share this answer
 
v2
Comments
Member 12677926 23-Mar-17 4:47am    
its nt wrking
Member 12677926 23-Mar-17 10:34am    
i had done through vector

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