Window Style changing Question with SetWindowLong

This topic is 4768 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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);



Share on other sites
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.

Share on other sites
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

Share on other sites
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.

Share on other sites
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.

Share on other sites
Quote:
 Original post by KippesoepThe 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?

Share on other sites
Quote:
Original post by GetWindowRect
Quote:
 Original post by KippesoepThe 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).

Share on other sites
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.

1. 1
2. 2
3. 3
frob
12
4. 4
5. 5
Rutin
10

• 13
• 14
• 65
• 14
• 15
• Forum Statistics

• Total Topics
632130
• Total Posts
3004284

×