Link to home
Start Free TrialLog in
Avatar of cjm20
cjm20Flag for United States of America

asked on

CListbox "Sort" property: how to change it during runtime

I have a CListbox control on a CDialog in my VC++ MFC application.  In the resource editor (ie, in design time), I have the "Sort" property intentionally set to FALSE.    However, I'd like to be able to change the value of the CListbox "Sort" property to TRUE while the program is running. For example, if the user clicks a button on the dialog, change the "Sort" property to TRUE.

I know I could "manaully" sort the contents of the CListbox at runtime (load them into a CString array, alphabetize them, clear the CListbox contents, and then reload the now-alphabetized contents in the CString array back into the CListbox).  Tha'ts fine.  I'd also just like to know how to programmatically set the "Sort" property.

Thanks very much!
Avatar of jkr
jkr
Flag of Germany image

That's actually pretty straightfoward, you just need to set 'LBS_SORT' via 'SetWindowLong()' (http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx):

HWND hwndListBox =   GetDlgItem( hDlg, ID_MY_LISTBOX ); 
DWORD dwStyle = GetWindowLong(hwndListBox , GWL_STYLE);
dwStyle = dwStyle | LBS_SORT; // enable 'sort'
SetWindowLong( hwndListBox , GWL_STYLE, dwStyle ); // apply it

Open in new window

BTW, you can also disable that the same way:

HWND hwndListBox =   GetDlgItem( hDlg, ID_MY_LISTBOX ); 
DWORD dwStyle = GetWindowLong(hwndListBox , GWL_STYLE);
dwStyle = dwStyle & ~LBS_SORT; // disable 'sort'
SetWindowLong( hwndListBox , GWL_STYLE, dwStyle ); // apply it
                                            

Open in new window

As a side note - you can do that also by deriving from CListBox and adding the above code as methods. Then you can obtain the window handle via 'GetSafeHwnd()' (inherited from CWnd).
To extend my lat comment, that could be like


class CMySortEnableableListBox : public CListBox
{
// other stuff...
public:

  void EnableSort()
  {
    HWND hwnd =   GetSafeHwnd();
    DWORD dwStyle = GetWindowLong(hwnd , GWL_STYLE);
    dwStyle = dwStyle | LBS_SORT; // enable 'sort'
    SetWindowLong( hwnd , GWL_STYLE, dwStyle ); // apply it                                          
  }

  void DisableSort()
  {
    HWND hwnd =   GetSafeHwnd();
    DWORD dwStyle = GetWindowLong(hwnd , GWL_STYLE);
    dwStyle = dwStyle & L+BS_SORT; // disable 'sort'
    SetWindowLong( hwnd , GWL_STYLE, dwStyle ); // apply it                                          
  }

// more stuff
};

Open in new window

Do you have any further questions regarding detaild on that issue?
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cjm20

ASKER

thanks Sara, and thanks JKR.  I tried setting the sort property as you'd described, using SetWindowLong(....).  No compile errors of course, but it also did not affect the sorting in the list box.  So the long way to go (two listboxes or dynamic creation / deletion of the list box) is going to be the way to go for me.