List control sort problem

When using a List control in a dialog based application I (with just a little C++ experience:) ran into something strange. The implementation should be straightforward and pretty simple and it was except for one thing.

When I sort the items in descending order by setting the descending property of the List control the last item overwrites the first one (displaying only 4 out of 5 items) when using the small icon view. When I sort the items in ascending order all items are sorted and displayed as they should.
If I set the list view instead of the small icon view descending as well as ascending sort work well.

What's wrong?

here's the code:
I added a CImageList data member "m_imageList" to the Dialog class and created an image list in the constructor "m_imageList.Create(16,16,0,10,5);"

The list control's initialization in the OnInitDialog() memberfunction looks like this:

HICON hIcon;
hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);
CListCtrl* pList=(CListCtrl*)GetDlgItem(IDC_LIST1);
pList->SetImageList(&m_imageList,LVSIL_SMALL);

static char* s[]={"axx","bxx","cxx","dxx","exx"};
     
for (int i(0); i<5; i++)
{
     m_imageList.Add(hIcon);
     pList->InsertItem(i,s[i],i);
}

--------------
So, descending sort displays axx,dxx,cxx,bxx and ascending sort displays axx,bxx,cxx,dxx,exx.
schuurbiersAsked:
Who is Participating?
 
ZoppoCommented:
So, then I understood it correctly ... I think you can use even 'report' list with one column and w/o header.

>Btw isn't this a bug in the CListctrl class?
hmm, with MS-products it's often hard to differentiate between bug and feature    :)
0
 
ZoppoCommented:
hmm, that's strange, for I tested it on my m/c and it works as expected...

what OS and VC do you use?
0
 
schuurbiersAuthor Commented:
Win98 and VC6.0
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
ZoppoCommented:
Well, I can't help you since I can't reproduce this (Win2k with VC 6.0) ... even
can't find anything about such problem in MSDN. Sorry...
0
 
schuurbiersAuthor Commented:
It's a weird thing I agry .....
Thanks for trying anyway!

btw is there a way to arrange the "list" view in just one column? I use the "small icon" view only because this view displays the items in just one column with a vertical scoll bar wich is what I want.
0
 
ZoppoCommented:
Hmm, I don't understand exactly what you mean with the last question, but
another way then using 'small icon' would be to use 'report' with one column
and without header.

But, I think maybe I found solution to you problem: As I'm used to create columns
for listcontrols (coz I usually use 'report' list controls) I did this without taking care
that in your code it isn't done. Now, at least for 'small icons' list control I saw on
my machine that sorting is incorrect if I don't create a column (I get correct number
of items but first is 'axx' instead of 'exx') ... when I do add a column it sorts correct, i.e.:

HICON hIcon;
hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);
CListCtrl* pList=(CListCtrl*)GetDlgItem(IDC_LIST1);
m_imageList.Create(16,16,0,10,5);
pList->SetImageList(&m_imageList,LVSIL_SMALL);
pList->InsertColumn( 0, "Test" ); // addin this makes sorting work correct for 'small icons'
static char* s[]={"axx","bxx","cxx","dxx","exx" };

for (int i(0); i<5; i++)
{
 m_imageList.Add(hIcon);
 pList->InsertItem(i,s[i],i);
}

// this is only used if you want to use 'report' lit control to adjust columns width depending
// on need of scrollbar
CRect rect;
pList->GetClientRect( rect );
pList->SetColumnWidth( 0, rect.Width() );    


I hope this helps,

ZOPPO
0
 
schuurbiersAuthor Commented:
Hi Zoppo!

>Hmm, I don't understand exactly what you mean with the last question,

Well, the "list" view of a List control arranges the items in multiple columns and a horizontal scroll bar while the "small icon" view arranges the items in one column and a vertical scroll bar which is what I want.

But I'm glad you could reproduce the same sort error I ran into. And inserting a column to the List control (but why should you if it's already there?) indeed solved the sort error.
Thanks!

Btw isn't this a bug in the CListctrl class?
0
 
schuurbiersAuthor Commented:
guess you're right about that last comment :)
thanks again ...
0
 
ZoppoCommented:
you're welcome,

have a nice day,

regards,

ZOPPO
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.