tags:

views:

202

answers:

3

I have a CListCtrl with plenty of room for all of the items, and they all display correctly --- until selected! As soon as any entry is selected, the end of that entry is truncated and an ellipsis is added:

Click for Image

I have no idea why this is happening. You can't see it in this image, but even very short entries show this behavior, even if the entries above or below are much longer and display fully. Here's the .rc code that created the control (and dialog):

IDD_COMBOBOX_DIALOG DIALOGEX 0, 0, 320, 200
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | 
    WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "ComboBox"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,263,7,50,16
    PUSHBUTTON      "Cancel",IDCANCEL,263,25,50,16
    CONTROL         "",IDC_LIST1,"SysListView32",LVS_REPORT | LVS_SINGLESEL | 
                    LVS_SORTASCENDING | LVS_ALIGNLEFT | LVS_NOSORTHEADER | WS_BORDER | 
                    WS_TABSTOP,7,78,306,85
END

and here's the code from InitDialog() that sets up and populates the CListCtrl:

myListCtrl.InsertColumn(0,_T("Allergies"));
FILE *f = fopen("c:\\allergies.txt", "r");
char sz[100];
if (f)
 while (fgets(sz,100,f))
  myListCtrl.InsertItem(0, sz);
if (f)
 fclose(f);
myListCtrl.SetColumnWidth(0,LVSCW_AUTOSIZE);
LVFINDINFO FI;
FI.flags = LVFI_PARTIAL|LVFI_STRING;
FI.psz = _T("A");
int i = myListCtrl.FindItem(&FI);
myListCtrl.SetItemState(i, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
myListCtrl.EnsureVisible(i, FALSE);

This one is making me really crazy. Any tips would be MUCH appreciated! Thanks for having a look.

+1  A: 

I'd try adding

myListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT);

before the InsertColumn line and see if that helps.

Fat Elvis
BingBingBing! We have a winner! That did the trick. Now --- doesn't this seem like a bug?! Why on earth would I want this bizarre default behavior? Ugh.
P.S. I added this line as the last line in my InitDialog(), but it worked fine.
You should actually use `myListCtrl.SetExtendedStyle(myListCtrl.GetExtendedStyle() | LVS_EX_FULLROWSELECT);` just in case any other LVS_EX_* styles have been set at some point. It may not be significant this time, but it's a good habit which could prevent future errors.
TheUndeadFish
A: 

MSDN doesn't seem to say if SetColumnWidth forces the contents to be redrawn automatically.

Windows programmer
A: 

I think the strings you enter into the list are not right-trimmed. Try

while (fgets(sz,100,f))
{
   CString s(sz);    
   s.TrimRight();
   myListCtrl.InsertItem(0, s);
}
dwo