I have a ListView control with 4 columns that is initialized in the WM_CREATE proc.
hListView1 = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, NULL, WS_CHILD|WS_VSCROLL|WS_HSCROLL|WS_VISIBLE|LVS_REPORT|LVS_SHOWSELALWAYS, 230, 20, 300, 250, hwnd, (HMENU)ID_EDIT1, GetModuleHandle(NULL), NULL);
ListView_SetExtendedListViewStyle(hListView1, LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
lvCol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvCol.fmt = LVCFMT_LEFT;
lvCol.iSubItem=0;
lvCol.cx=30;
lvCol.pszText="";
ListView_InsertColumn(hListView1, 0, &lvCol);
lvCol.iSubItem=1;
lvCol.cx=150;
lvCol.pszText="Name";
ListView_InsertColumn(hListView1, 1, &lvCol);
lvCol.iSubItem=2;
lvCol.cx=50;
lvCol.pszText="Size";
ListView_InsertColumn(hListView1, 2, &lvCol);
lvCol.iSubItem=3;
lvCol.cx=80;
lvCol.pszText="Modified";
ListView_InsertColumn(hListView1, 3, &lvCol);
Then i have a function that will insert the items (it works fine until i call deleteallitems)
...
LVITEM lvItem;
j = 0;
while(FindNextFile(hFind,&FindFileData)){
lvItem.iItem = j;
lvItem.iImage = 1;
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
lvItem.iImage = 0;
}
ListView_InsertItem(hListView1, &lvItem);
ListView_SetItemText(hListView1, j, 1, FindFileData.cFileName);
ListView_SetItemText(hListView1, j, 2, msg1);
ListView_SetItemText(hListView1, j, 3, msg2);
j++;
}
But then whenever i call
ListView_DeleteAllItems(hListView1);
if after i call my function that insert items, my listview is cleared (the columns are still there) but no new items are insered..
I heard about indexes that are not cleared but i couldnt figured it out.
Thanks in advance 😉
Solution :
Added
lvItem.mask = LVIF_IMAGE | LVIF_STATE;
lvItem.state = 0;
lvItem.stateMask = 0;
lvItem.iSubItem = 0;
You are not setting
lvItem.mask, soListView_InsertItemdoesn’t know which fields are valid and which aren’t.Try something like this: