# Adding Subitems to Listviews

This topic is 4440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm hoping someone can give me a hand with my Win SDK code. I swear that I'm doing everything correctly, but for some reason, my Listview window simply refuses to show subitems. Even googling this, I seem to be doing what everyone else does (as far as I can tell). I'm sure that I'm just making some dumb mistake, but I just don't see it :) Here's the code: This creates the window and sets up the columns. This part seems to work fine as far as I can tell, I have a window with two columns on screen, properly labelled
    LV_COLUMN NewColumn;

//create the child windows
FileDisplay = CreateWindowEx(NULL, WC_LISTVIEW, "FileDisplay", WS_CHILD | WS_BORDER | LVS_REPORT,
20, 280, 350, 250,MasterWindowHandle, (HMENU)ID_DFBS_FILEDISPLAY,
Instance, NULL);
//set up the columns on the file display window
NewColumn.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
NewColumn.cx = 150;
NewColumn.iSubItem = 0;
NewColumn.pszText = "File";
ListView_InsertColumn(FileDisplay, 0, &NewColumn);
NewColumn.cx = 200;
NewColumn.iSubItem = 1;
NewColumn.pszText = "Date";
ListView_InsertColumn(FileDisplay, 1, &NewColumn);

Later on I want to fill in this list, so here is what I'm doing (the code to detect files is not yet finished, I want to get the first file in there before going on to FindNextFile)
void DataFileBrowseScreenClass::UpdateFileList()
{
HANDLE FileHandle;
WIN32_FIND_DATA FindData;
LV_ITEM NewItem;
int Index;

ListView_DeleteAllItems(FileDisplay);

NewItem.iItem = 0;
NewItem.iSubItem = 0;

FileHandle = FindFirstFile("*.RAW", &FindData);
if (FileHandle == INVALID_HANDLE_VALUE)
FindClose (FileHandle);
else
{
NewItem.pszText = FindData.cFileName;
Index = ListView_InsertItem(FileDisplay, &NewItem);
ListView_SetItemText(FileDisplay, Index, 1, "Testing");
}
FindClose (FileHandle);
}

As you can see, the first column ought to be a file name, the second is just a test string (for now). What happens is the first column gets the file name just fine. However, the second remains blank. if I change the line ListView_SetItemText(FileDisplay, Index, 1, "Testing"); to ListView_SetItemText(FileDisplay, Index, 0, "Testing"); the file name is replaced by the word "Testing" in the first column, so I know it's really getting to that line. I've also tried using ListView_SetItem() with the same results. Anyone see what I'm doing wrong? Thanks, Ron

##### Share on other sites
Try this:
...
{
ListView_InsertItem(FileDisplay, &NewItem);
ListView_SetItemText(FileDisplay,0,1,"Testing");
ListView_SetItemText(FileDisplay,0,0,FindData.cFileName);
}
...

Perhap this source will help you. It is written using Borland's OWL which is a wrapper around WIN32 API data and functions. Shouldn't be too hard to decipher.
voidTClientWindow::SetupWindow(){  TWindow::SetupWindow();  ListWind->SetTextBkColor( TColor::Sys3dFace );  ListWind->SetImageList(*LgImageList, TListWindow::Normal);  ListWind->SetImageList(*SmImageList, TListWindow::Small);//SET UP 4 COLUMNS IN THE LISTVIEW WINDOW//NOTE: TListWindColumn is a wrapper around the LV_COLUMN type  TListWindColumn column("Name(1)", 200);  column.SetSubItem(0);  ListWind->InsertColumn(0, column);   TListWindColumn column2("Type(2)", 80,TListWindColumn::Right);   column2.SetSubItem(1);   ListWind->InsertColumn(1, column2);   TListWindColumn column3("Size(3)", 120);   column3.SetSubItem(2);   ListWind->InsertColumn(2, column3);   TListWindColumn column4("Modified(4)", 200);   column4.SetSubItem(3);   ListWind->InsertColumn(3, column4);   ListWind->DeleteAllItems();//CREATE 8 ROWS IN THE LISTVIEW//NOTE: SmImageList->GetImageCount() returns 8  for (int i = 0; i < SmImageList->GetImageCount(); i++) {    TListWindItem item;//NOTE:This is a wrapper around LV_ITEM    item.SetStateImage( 0 );    item.SetImageIndex(i);    item.SetSubItem(0); //set column number for this item    item.SetIndex(i);	//set row of this item    ListWind->InsertItem(item);//FILL EACH COLUMN IN THE CURRENT ROW     	for(int j = 0;j < 4;j++)//ITERATE THROUGH EACH COLUMN IN THIS ROW      {      	static char msg[64];         sprintf(msg,"Col:%i Row:%i",j,i);         ;//NOTE:We can set an item text two ways (either will work)         #if 1         //make sure row index is set (item.SetIndex(i)) as we have above this loop  			item.SetText( msg );      	item.SetSubItem( j+0 );//column      	ListWind->SetItem( item );         #else         ListWind->SetItemText(i,j+0,msg);//row,column,text         #endif      }  }//PRETTY IT UPListWind->SetExtStyle(0,LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);  ListWind->SetBkgndColor( TColor::Black);  ListWind->SetTextBkColor(TColor::Black);  ListWind->SetTextColor(TColor::LtYellow);ListWind->SetItemState(ListWind->GetItemCount()-1,LVIS_SELECTED,LVIS_SELECTED);}

##### Share on other sites
Thanks for the suggestion Jack. Unfortunately, it didn't work :)

But here was an odd thing. I left the test program running while I went to lunch. When I came back, my screen saver had started up. When I then input my password, much to my surprise, my 2nd column had magically appeared with "testing" all the way down the column! (I have since added more data with FindNextFile(), so there is more than just the one entry in the first column, there are more like 20 now).

Thinking the code elves had fixed my program, I shut it down and ran it again. No column. Then I set my screen saver for 1 minute, ran the program, let the screensaver come up, and when I unlocked the computer again, there was the 2nd column with "testing" in all 20 rows!

Now I find this very odd, because nothing else seems to work. Minimizing and remaximizing the app won't bring up the column, nor will putting a different program in front (to invalidate the window rect).

Just in case, I added a RedrawWindow() at the end of the subroutine, but that didn't work either. It seems like there is some kind of refresh issue here. The text seems to be there, but it's not showing up until the screensaver comes up. I've never seen anything like this before. Anyone ever see anything similar happen, and how do I fix it?

##### Share on other sites
Nevermind all, I figured it out.

As I figured, I did a dumb thing. Due to the way my initialization was set up, I was adding the items and subitems to my ListView box before the columns were set up. I'm actually a little surprised I didn't get a AV error.

Swapped them around the right way, and everything works peachy. Sorry to cry wolf :)

Ron

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002065
×