Solved

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

Posted on 2014-03-06
7
1,065 Views
Last Modified: 2014-03-14
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!
0
Comment
Question by:cjm20
  • 5
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39910981
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

0
 
LVL 86

Expert Comment

by:jkr
ID: 39911093
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

0
 
LVL 86

Expert Comment

by:jkr
ID: 39911251
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).
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:jkr
ID: 39911462
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

0
 
LVL 86

Expert Comment

by:jkr
ID: 39914223
Do you have any further questions regarding detaild on that issue?
0
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 39916457
the LBS_SORT flag cannot be added after the listbox was created. even with code like

void CTestdlgDlg::OnBnClickedButton1()
{
    // TODO: Add your control notification handler code here

    DWORD dwStyle = GetWindowLong(m_listbox.m_hWnd , GWL_STYLE);
    dwStyle = dwStyle | LBS_SORT; // enable 'sort'
    SetWindowLong( m_listbox.m_hWnd , GWL_STYLE, dwStyle ); // apply 
    m_listbox.ResetContent();
    m_listbox.AddString("XYZ");
    m_listbox.AddString("ABC");
    m_listbox.AddString("DEF");
    m_listbox.AddString("CCC");

}

Open in new window


you would not be able to get the strings displayed sorted if the sort flag was not set initially in the resources or at create time.

a way out is to have a pointer to CListBox in your dialog and create the listbox on the fly (dynamically) and if the sort needs to be switched, you would destroy the control and create a new one with the sort flag given. an easier way (probably) is to have the listbox twice, one is hidden and sorted and one is visible and not sorted. both listbox controls are at the same place and you disable-hide or show-enable the one or the other.

my way would be to use a list control instead of a listbox. here the list may be unsorted initially but would be sorted if the user clicks into the list header.

Sara
0
 

Author Closing Comment

by:cjm20
ID: 39930198
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 78
How To Loop - Python 19 79
Re-position sub-options beneath the TAB 7 75
How to remove Recent Projects from Embarcadero C++ builder XE10. Berlin 2 31
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This video teaches viewers about errors in exception handling.

912 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

23 Experts available now in Live!

Get 1:1 Help Now