-
March 27th, 2009, 09:41 AM
#1
how to dynamically set sort/!sort on listbox
Hi,
I'm trying to change the sorting/not sorting style of listbox in runtime.
Code example
Code:
HWND hListItemsList = GetDlgItem( hDlg, ID_DLG_ITEMLIST );
DWORD style = GetWindowLong(hListItemsList, GWL_STYLE);
style = style|LBS_SORT;
SetWindowLong( hListItemsList, GWL_STYLE, style );
SetWindowPos( hListItemsList, hListItemsList, 10, 10, 10, 10, SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE); /// <<< this was supposed to help - FAILED
SendMessage( hListItemsList, LB_RESETCONTENT, 0, MAKELPARAM(10, 0));
for( size_t i=0; i<g_vecScannedItems.size(); ++i )
{
SendMessage( hListItemsList, LB_ADDSTRING, 0, (LPARAM)g_vecScannedItems[i].c_str() );
} // for
However after this trick, items doesn't seem to be sorted. Any ideas?
/J
-
March 29th, 2009, 01:49 AM
#2
Re: how to dynamically set sort/!sort on listbox
Unfortunately, few common control styles cannot be changed after creation.
LBS_SORT is one of them.
-
March 29th, 2009, 03:55 PM
#3
Re: how to dynamically set sort/!sort on listbox
OK, good to know that.
BTW maybe you know why, when I'm creating the listbox from a scratch the size of item fonts are bigger that the ones used in original listbox.
Where or how change the size of it?
I was trying to use LB_SETITEMHEIGHT but without any effect.
-
March 30th, 2009, 01:28 PM
#4
Re: how to dynamically set sort/!sort on listbox
When you create whatever control by calling a Create... function it has not an associated font.
The system will use a default font, one which may be larger than one set when your control was created from a resouece.
Take a look at WM_SETFONT.
-
March 31st, 2009, 02:12 AM
#5
Re: how to dynamically set sort/!sort on listbox
A solution to your sorting issue is that you create 2 listboxes. One is sorted and the other one is not sorted. Then just show/hide the listbox that you want.
-
March 31st, 2009, 08:37 AM
#6
Re: how to dynamically set sort/!sort on listbox
That's the way I will follow. But I was hoping for sth more tricky
Thanks for help.
/J
-
April 1st, 2009, 01:12 AM
#7
Re: how to dynamically set sort/!sort on listbox
But I was hoping for sth more tricky
Okay, there it is: You create an owner-drawn listbox of LBS_SORT style. Following this way you have to implement a comparison function (in fact it's a handler of WM_COMPAREITEM message), where you provide according to current settings any sorting algorythm you fancy.
Last edited by Igor Vartanov; April 1st, 2009 at 01:27 AM.
Best regards,
Igor
-
June 21st, 2013, 05:00 AM
#8
Re: how to dynamically set sort/!sort on listbox
Speaking in MFC Terms: Try using InsertString() for the non-sorted fill operation, use AddString() for the sorted fill operation.
I guess InsertString is the message LB_INSERTSTRING. See http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
LB_INSERTSTRING message does not cause a list with the LBS_SORT style to be sorted
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|