how to dynamically set sort/!sort on listbox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: how to dynamically set sort/!sort on listbox

  1. #1
    Join Date
    Sep 2007
    Location
    poland|wrocław
    Posts
    47

    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

  2. #2
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,019

    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.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    Join Date
    Sep 2007
    Location
    poland|wrocław
    Posts
    47

    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.

  4. #4
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,019

    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.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,062

    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.

  6. #6
    Join Date
    Sep 2007
    Location
    poland|wrocław
    Posts
    47

    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

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,927

    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

  8. #8
    Join Date
    Jul 2011
    Posts
    2

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center