CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  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
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  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
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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

    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.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  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
    6,620

    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
  •  





Click Here to Expand Forum to Full Width

Featured