Window Style changing Question with SetWindowLong

Started by
6 comments, last by GetWindowRect 18 years, 8 months ago
I am trying to change a listbox's sorted style to either be there or not. I use SetWindowLong and have tried SetWindowLongPtr but neither seem to change the style. I was wondering if I am doing something wrong or if this particular style cannot be changed. Here is the code I have used so far:

m_Sorted = sort;

long style = GetWindowLong(m_hWnd, GWL_STYLE);
if (m_Sorted) 
	style |= LBS_SORT;
else 
	style &= ~(LBS_SORT);


SetWindowLong(m_hWnd, GWL_STYLE, style);
	
InvalidateRect(m_hWnd, NULL, false);
UpdateWindow(m_hWnd);


Advertisement
I'm not sure exactly how LBS_SORT works, but it may be that it sorts on insertion, rather than at draw-time.

Try clearing the listbox or combobox (or whatever it is) with LB_/CB_RESETCONTENT and then re-inserting the elements after the style change.
I just tried before adding anything to the listbox to change the style to include LBS_SORT, and when the items loaded, it was still unsorted. Thank you for the idea though, that was something I haven't thought to try
SetWindowLong Function

Quote:
Remarks

Certain window data is cached, so changes you make using SetWindowLong will not take effect until you call the SetWindowPos function. Specifically, if you change any of the frame styles, you must call SetWindowPos with the SWP_FRAMECHANGED flag for the cache to be updated properly.
...


Here's a VB example of modifying a window's style.
Here's a C# example of modifying a window's style.

I haven't been able to find a C++ version. The C# version shouldn't be too difficult to adapt.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
The LBS_SORT style is checked only at control creation time. Same goes for the LBS_MULTIPLESEL style and others. SetWindowPos won't help here.
Kippesoep
Quote:Original post by Kippesoep
The LBS_SORT style is checked only at control creation time. Same goes for the LBS_MULTIPLESEL style and others. SetWindowPos won't help here.


Probably a stupid question but thought I would double check... So this means I cannot (without deleting the listbox and recreating it) change the sort style after it has been created?
Quote:Original post by GetWindowRect
Quote:Original post by Kippesoep
The LBS_SORT style is checked only at control creation time. Same goes for the LBS_MULTIPLESEL style and others. SetWindowPos won't help here.


Probably a stupid question but thought I would double check... So this means I cannot (without deleting the listbox and recreating it) change the sort style after it has been created?


It looks like it that is the case.

You could try using the slightly more complex but powerful ListView control, and use a hidden 'dummy' column to record the original input order. Then, use the ListView_SortItems() function with an appropriate comparison function to either sort by original input order ('unsorted') or by value ('sorted').

Another would be to clear the listbox, and either reinsert the items in whatever order they come in, or sort them yourself (using an stl container and std::sort would save you having to write your own sort function).
Thank you a bunch for all your help, I am still new to c++, only been playing around with it for a couple weeks really (window programming wise), but I will look into the ListView control. I use VB mainly but would like to get into c++, it is a lot more fun, and I can learn how things actually work.

This topic is closed to new replies.

Advertisement