Solved

List control sort problem

Posted on 2001-08-29
9
788 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:schuurbiers
  • 5
  • 4
9 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 6436428
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
 

Author Comment

by:schuurbiers
ID: 6436481
Win98 and VC6.0
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 6436745
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
 

Author Comment

by:schuurbiers
ID: 6439471
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 31

Expert Comment

by:Zoppo
ID: 6439544
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
 

Author Comment

by:schuurbiers
ID: 6439582
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
 
LVL 31

Accepted Solution

by:
Zoppo earned 100 total points
ID: 6439745
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
 

Author Comment

by:schuurbiers
ID: 6439779
guess you're right about that last comment :)
thanks again ...
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 6439790
you're welcome,

have a nice day,

regards,

ZOPPO
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
unable to delete all specified values regedit 38 278
ShiftLeft challenge 21 76
withoutTen challenge 14 100
wordcount challenge 11 108
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now