Since it seems the ListView control had no real way to move items, I attempted to write my own. It's not working. It moves the item, but the subitems of the new item are all blank.
Here's the code...
The message boxes are for debugging...Code:/* * Moves the selected list view item up or down. * hwnd = list view * change = -1 for up 1, 1 for down 1, -2 for up two, etc. * Returns 0 on success, nonzero on failure. */ int ListView_Move(HWND hwnd, int change) { LV_ITEM lvi = {0}; int iOld, iNew = -1; char text_buf[1024]; int iColumn, nColumns, ret; if(!change) return 1; if(change > 0) ++change; iOld = ListView_GetNextItem(hwnd, -1, LVNI_SELECTED); if(iOld == -1) return 1; lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_PARAM; lvi.iItem = iOld; lvi.pszText = text_buf; lvi.cchTextMax = 1024; // Create the new item, with all the info of the old... ListView_GetItem(hwnd, &lvi); lvi.iItem += change; iNew = ListView_InsertItem(hwnd, &lvi); // If we create an item above the old one, then the old one moves down one... if(change < 0) ++iOld; nColumns = Header_GetItemCount(ListView_GetHeader(hwnd)); sprintf(text_buf, "%d columns...", nColumns); MessageBox(GetParent(hwnd), text_buf, "", 0); // Copy all the subitems from the old to the new for(iColumn = 1; iColumn < nColumns; ++iColumn) { ZeroMemory(&lvi, sizeof(LV_ITEM)); lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_PARAM; lvi.iItem = iOld; lvi.iSubItem = iColumn; lvi.pszText = text_buf; lvi.cchTextMax = 1024; ListView_GetItem(hwnd, &lvi); MessageBox(GetParent(hwnd), text_buf, "", 0); lvi.iItem = iNew; ret = ListView_SetItem(hwnd, &lvi); sprintf(text_buf, "%d = ret... (iNew = %d) last_err = %d?", ret, iNew, GetLastError()); MessageBox(hwnd, text_buf, "", 0); } ListView_DeleteItem(hwnd, iOld); return 0; }
The first one says there are 3 columns (correct), the second one should say what each subitem's text is (it does, so: correct), and the last tells other stuff. (The return value of ListView_SetItem, iNew (the index of the new item), and GetLastError().) The last messagebox says 0, a correct index value, and 0. GetLastError() is returning 0 (success) and ListView_SetItem is returning 0 (failure). (It apperantly doesn't call SetLastError...)
Why, though, does ListView_SetItem fail? I'm only changing the iItem member... the rest of it was filled in by ListView_GetItem().
Thanks in Advance for any help...



LinkBack URL
About LinkBacks


