Click to See Complete Forum and Search --> : Changing a list-view's order
QBasicer
July 27th, 2005, 07:02 PM
In my program, I have a list view that's in order on how the user wants to pack the files. This requires me to add a move up and move down button.
My question is, how to I change the order of these? Do I just bring up both of the items in question, then change the value of iItem?
Thanks!
EDIT: This is the code I tried after that didn't seem to work:
case BTN_MOVEUP:
{
int noOfFiles = ListView_GetItemCount(GetDlgItem(hDlg,LV_FILES));
int selected = ListView_GetSelectionMark(GetDlgItem(hDlg, LV_FILES));
if (selected > 0){
LVITEM lvIOrig;
LVITEM lvIOther;
lvIOrig.mask = LVIF_PARAM;
lvIOther.mask = LVIF_PARAM;
lvIOrig.iItem = selected;
lvIOther.iItem = selected - 1;
ListView_GetItem(GetDlgItem(hDlg,LV_FILES), &lvIOrig);
ListView_GetItem(GetDlgItem(hDlg,LV_FILES), &lvIOther);
FILEINFO *temp;
temp = (FILEINFO*)lvIOrig.lParam;
lvIOrig.lParam = lvIOther.lParam;
lvIOther.lParam = (LPARAM)temp;
ListView_SetItem(GetDlgItem(hDlg,LV_FILES),&lvIOrig);
ListView_SetItem(GetDlgItem(hDlg,LV_FILES),&lvIOther);
for (int i = 0; i < noOfFiles; i++){
ListView_Update(GetDlgItem(hDlg,LV_FILES),i);
}
}
}
break;
kirants
July 27th, 2005, 07:21 PM
Possibly the steps here will help:
http://www.codeguru.com/Cpp/controls/listview/advanced/article.php/c4203/
QBasicer
July 27th, 2005, 07:29 PM
Possibly the steps here will help:
http://www.codeguru.com/Cpp/controls/listview/advanced/article.php/c4203/
Unfortunatly I'm using lists by messages/macros, not a CListView.
kirants
July 27th, 2005, 07:37 PM
I understand, but if you look at the snippet, there are enough comments to understand the flow and traslate MFC calls to equivalent Win32 calls.
If you have trouble understanding any specific item, post here and people can help.
Note that, most MFC method calls of CListView are necessarily mere wrappers to Win32 calls like you use. There is no big magic that MFC does..
QBasicer
July 28th, 2005, 04:45 PM
I understand, but if you look at the snippet, there are enough comments to understand the flow and traslate MFC calls to equivalent Win32 calls.
If you have trouble understanding any specific item, post here and people can help.
Note that, most MFC method calls of CListView are necessarily mere wrappers to Win32 calls like you use. There is no big magic that MFC does..
Yes but the MFC seems just to copy the text, right? What about the the lParam that I need? I do my stuff with text-callback.
Here's another failed attempt:
LVITEM lvIOrig;
//LVITEM lvIOther;
lvIOrig.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_PARAM;
//lvIOther.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_PARAM;
lvIOrig.iItem = selected;
//lvIOther.iItem = selected - 1;
ListView_GetItem(GetDlgItem(hDlg,LV_FILES), &lvIOrig);
//ListView_GetItem(GetDlgItem(hDlg,LV_FILES), &lvIOther);
ListView_DeleteItem(GetDlgItem(hDlg, LV_FILES), selected);
lvIOrig.iItem = lvIOrig.iItem - 1;
ListView_InsertItem(GetDlgItem(hDlg, LV_FILES), &lvIOrig);
//FILEINFO *temp;
//temp = (FILEINFO*)lvIOrig.lParam;
ListView_SetItem(GetDlgItem(hDlg,LV_FILES),&lvIOrig);
for (int i = 0; i < noOfFiles; i++){
ListView_Update(GetDlgItem(hDlg,LV_FILES),i);
}
kirants
July 28th, 2005, 08:50 PM
Some sample code that I wrote:
void SwapItems(HWND hWndList,int nSourceIndex,int nDestIndex)
{
//now swap the items
//get source
char szSourceText[255],szDestText[255];
LVITEM lvSItem;
lvSItem.iSubItem = 0;
lvSItem.cchTextMax = 255;
lvSItem.iItem = nSourceIndex;
lvSItem.pszText = szSourceText;
lvSItem.mask = (LVIF_PARAM | LVIF_TEXT | LVIF_IMAGE);
ListView_GetItem(hWndList,&lvSItem);
//get dest
LVITEM lvDItem;
lvDItem.iSubItem = 0;
lvDItem.iItem = nDestIndex;
lvDItem.cchTextMax = 255;
lvDItem.pszText = szDestText;
lvDItem.mask = (LVIF_PARAM | LVIF_TEXT | LVIF_IMAGE);
ListView_GetItem(hWndList,&lvDItem);
//now swap
lvSItem.iItem = nDestIndex;
lvDItem.iItem = nSourceIndex;
ListView_SetItem(hWndList,&lvSItem);
ListView_SetItem(hWndList,&lvDItem);
}
void CTestListDlg::OnButtonUp()
{
// TODO: Add your control notification handler code here
//get selected item
int nSel = ListView_GetNextItem(GetDlgItem(IDC_LIST1)->GetSafeHwnd(),-1,LVNI_SELECTED);
//if reached top, no point
if(-1 != nSel && nSel)
{
SwapItems(GetDlgItem(IDC_LIST1)->GetSafeHwnd(),nSel,nSel - 1);
}
}
void CTestListDlg::OnButtonDown()
{
// TODO: Add your control notification handler code here
int nSel = ListView_GetNextItem(GetDlgItem(IDC_LIST1)->GetSafeHwnd(),-1,LVNI_SELECTED);
int nTotalItemCount = ListView_GetItemCount(GetDlgItem(IDC_LIST1)->GetSafeHwnd());
//if reached top, no point
if(-1 != nSel && nSel < nTotalItemCount)
{
SwapItems(GetDlgItem(IDC_LIST1)->GetSafeHwnd(),nSel,nSel + 1);
}
}
QBasicer
August 6th, 2005, 06:35 PM
Sorry for the bump and late response.
Thanks! That works perfectly.
kirants
August 7th, 2005, 02:11 AM
U r welcome. Good luck :wave:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.