Solved

List control sort problem

Posted on 2001-08-29
9
794 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

728 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