Solved

List control sort problem

Posted on 2001-08-29
9
785 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 30

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 30

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 30

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 30

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 30

Expert Comment

by:Zoppo
ID: 6439790
you're welcome,

have a nice day,

regards,

ZOPPO
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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