Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

When to CListBox::ModifyStyle( LBS_SORT, 0, 0 )?

718 views
Skip to first unread message

unformat

unread,
Jun 21, 1999, 3:00:00 AM6/21/99
to
Hi,
I have a CListBox as part of a resource-editor created dialog which has
been designed with the style LBS_SORT and I want to remove the LBS_SORT
style at runtime. I have added a call to the CWnd member fn()
ModifyStyle( LBS_SORT, 0, 0 ); within the dialog's OnInitDialog member
fn() an the listbox remains sorted -- is this the wrong place?

TIA

PS: CWnd::ModifyStyle just does...
style = GetWindowLong( whatever.m_hWnd, GWL_STYLE );
style &= ~( LBS_SORT );
SetWindowLong( whatever.m_hWnd, GWL_STYLE, style );
...which I have also tried here.

--
unfo...@my-deja.com
http://www.bmeworld.com/unformat/ -- see my ass!
"Enough love to knock a rhino sideways" - SNUFF


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

David Lowndes

unread,
Jun 21, 1999, 3:00:00 AM6/21/99
to
>I have a CListBox as part of a resource-editor created dialog which has
>been designed with the style LBS_SORT and I want to remove the LBS_SORT
>style at runtime.

Many Windows controls only use the style when the control is created.
Therefore, while you can change the style bits, the control blissfully
ignores them.

The work-around in these situations is to either destroy the original
control and create another in the style you want, or to maintain 2
controls, one of each style, and hide/show the appropriate one.

Dave
----
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.

Scott Tunstall

unread,
Jun 23, 1999, 3:00:00 AM6/23/99
to
Well I used ModifyStyle for my ListBox in my OnInitDialog() and it worked
just dandy for me.

Send me a mail and I'll forward you the (extremely simple) program I tested
it with.

Scott Tunstall

Get C64 -> PC Boulderdash code @
www.kwikrite.clara.net/bdash/

David Lowndes wrote in message <37718d88...@msnews.microsoft.com>...

David Lowndes

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to
>Well I used ModifyStyle for my ListBox in my OnInitDialog() and it worked
>just dandy for me.
>
>Send me a mail and I'll forward you the (extremely simple) program I tested
>it with.

Scott,

It doesn't seem to work for me. Are you sure that your list box
doesn't already have the sort style set in the dialog resource?

Rather than post an entire project, just show us the few lines of code
that works for you. Here's what I've tried (and doesn't work for me):

BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();

....

// TODO: Add extra initialization here
CListBox * pLB = (CListBox*) GetDlgItem( IDC_LIST1 );

pLB->AddString("E(top) String 1");
pLB->AddString("B String 2");
pLB->AddString("G String 3");
pLB->AddString("D String 4");
pLB->AddString("A String 5");
pLB->AddString("E(bottom) String 6");

/* This has no effect */
pLB->ModifyStyle( 0, LBS_SORT );
return TRUE;

Scott Tunstall

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to
Your code is *adding* the LBS_SORT style to the listbox. In the original
post you wanted to REMOVE the sort style.

CWnd::ModifyStyle is prototyped as:

BOOL ModifyStyle( DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0 );

Here's mine to remove the LBS_SORT style (from another program but it still
uses ModifyStyle):

HWND hWnd = ::GetDlgItem(GetSafeHwnd(), IDC_LIST1);

m_pListBox->Attach(hWnd);
m_pListBox->ModifyStyle(LBS_SORT,0,0);


And I rechecked my resource file, I can assure you the LBS_SORT style IS
there in my dialog, yet is successfully removed at run time (I used Spy++)


Hope this helps.
Scott

David Lowndes

unread,
Jun 24, 1999, 3:00:00 AM6/24/99
to

>Your code is *adding* the LBS_SORT style to the listbox. In the original
>post you wanted to REMOVE the sort style.

You're right Scott, my logic was backwards to the original posters
question, however (for me at least) it doesn't change anything, I
can't get a listbox to change its sorting at run-time.

>And I rechecked my resource file, I can assure you the LBS_SORT style IS
>there in my dialog, yet is successfully removed at run time (I used Spy++)

The style flag does indeed change, I don't dispute that. However, the
control doesn't change its behaviour. The following code attached to a
button on the dialog should illustrate the situation if you care to
try it, I'd be interested in your results.

void CMyDlg::OnButton1()
{
static bSort = false;

CListBox * pLB = (CListBox*) GetDlgItem( IDC_LIST1 );

/* Clear the list, change the style, and re-add the items
* just to be sure sorting doesn't only work on adding.
*/
pLB->ResetContent();

if ( bSort )
{
pLB->ModifyStyle( LBS_SORT, 0 );
}
else
{


pLB->ModifyStyle( 0, LBS_SORT );
}

bSort = !bSort;

pLB->AddString("E(top) String 1");
pLB->AddString("B String 2");
pLB->AddString("G String 3");
pLB->AddString("D String 4");
pLB->AddString("A String 5");
pLB->AddString("E(bottom) String 6");
}

Dave

Scott Tunstall

unread,
Jun 28, 1999, 3:00:00 AM6/28/99
to
David, you have messed up.

Can you see what's wrong here?

> if ( bSort )
> {
> pLB->ModifyStyle( LBS_SORT, 0 );
> }
> else
> {
> pLB->ModifyStyle( 0, LBS_SORT );
> }

The code is like so:

IF YOU WANT TO SORT THEN
REMOVE SORT STYLE FROM LISTBOX (The first param of ModifyStyle is
dwRemove!!!)
ELSE IF YOU DON'T WANT SORT THEN
ADD SORT STYLE


Do you see what's wrong? Here's the revised code, and it works on mine (I
can send you the workspace and files if you want).

CListBox * pLB = (CListBox*) GetDlgItem( IDC_LIST1 );

/* Clear the list, change the style, and re-add the items
* just to be sure sorting doesn't only work on adding.
*/
pLB->ResetContent();

if ( bSort )
{
pLB->ModifyStyle( 0, LBS_SORT );
}
else


{
pLB->ModifyStyle( LBS_SORT, 0 );
}

pLB-RedrawWindow();

Scott Tunstall

unread,
Jun 28, 1999, 3:00:00 AM6/28/99
to
Forget that last post, even though the params in ModifyStyle were in the
wrong place, my bloody resource file never saved itself (I ran before
building ) and so I got the wrong result.

Shit.

Oh well, it looks like the only way to get round this is (unfortunately) to
destroy the current window and create a new one with the required sort style
in it's place.

Scott


David Lowndes wrote in message <3779bdf9...@msnews.microsoft.com>...


>
>>Your code is *adding* the LBS_SORT style to the listbox. In the original
>>post you wanted to REMOVE the sort style.
>
>You're right Scott, my logic was backwards to the original posters
>question, however (for me at least) it doesn't change anything, I
>can't get a listbox to change its sorting at run-time.
>
>>And I rechecked my resource file, I can assure you the LBS_SORT style IS
>>there in my dialog, yet is successfully removed at run time (I used Spy++)
>
>The style flag does indeed change, I don't dispute that. However, the
>control doesn't change its behaviour. The following code attached to a
>button on the dialog should illustrate the situation if you care to
>try it, I'd be interested in your results.
>
>void CMyDlg::OnButton1()
>{
> static bSort = false;
>

> CListBox * pLB = (CListBox*) GetDlgItem( IDC_LIST1 );
>
> /* Clear the list, change the style, and re-add the items
> * just to be sure sorting doesn't only work on adding.
> */
> pLB->ResetContent();
>
> if ( bSort )
> {

> pLB->ModifyStyle( LBS_SORT, 0 );
> }
> else
> {
> pLB->ModifyStyle( 0, LBS_SORT );
> }

> bSort = !bSort;
>
> pLB->AddString("E(top) String 1");
> pLB->AddString("B String 2");
> pLB->AddString("G String 3");
> pLB->AddString("D String 4");
> pLB->AddString("A String 5");
> pLB->AddString("E(bottom) String 6");
>}
>

David Lowndes

unread,
Jun 29, 1999, 3:00:00 AM6/29/99
to
>Oh well, it looks like the only way to get round this is (unfortunately) to
>destroy the current window and create a new one with the required sort style
>in it's place.

Whew, thanks for that confirmation Scott, I was beginning to doubt my
sanity.

Scott Tunstall

unread,
Jun 29, 1999, 3:00:00 AM6/29/99
to
Yep, if you look at the AddString documentation it does mention the fact
that "if the ListBox was CREATED with the LBS_SORT style..." .

I'm surprised at this, you would have thought removing the sort style and
then redrawing the window would have the desired effect?

Aha! Wait a minute, there is a way around this but it is known as "el long
wayo" in Spanish <grin>. You could implement an "owner draw" listbox... but
to my mind this is overkill.

Maybe you could make a million from writing an OCX version? If you do, I
want royalties for the idea :)

I wish you all the best

Scott

David Lowndes wrote in message <37817954...@msnews.microsoft.com>...

David Lowndes

unread,
Jun 30, 1999, 3:00:00 AM6/30/99
to
>Maybe you could make a million from writing an OCX version?

I somehow doubt it. There's probably a good helping of them available
already - I usually find I'm never first with a good idea :(.

Joseph M. Newcomer

unread,
Jun 30, 1999, 3:00:00 AM6/30/99
to
Just a footnote: if you write GetDlgItem more than once a year you are not
using MFC properly. Use the ClassWizard to create a control-type variable,
so you have a declaration like
CListBox c_List1;

added to your .h file. Then, instead of the gross GetDlgItem, you can write

c_List1.ModifyStyle(...);

I use a c_ prefix to indicate control variables and m_ is reserved for data
value variables. That way you can use the same name suffix and know what you
are using it for. OTOH, I rarely use data variables to initialize controls,
since it is rare that a simple copy suffices.
joe
Scott Tunstall <tuns...@email.msn.com> wrote in message
news:Oxxx9aaw#GA.309@cpmsnbbsa03...


> David, you have messed up.
>
> Can you see what's wrong here?
>

> > if ( bSort )
> > {
> > pLB->ModifyStyle( LBS_SORT, 0 );
> > }
> > else
> > {
> > pLB->ModifyStyle( 0, LBS_SORT );
> > }
>

> The code is like so:
>
> IF YOU WANT TO SORT THEN
> REMOVE SORT STYLE FROM LISTBOX (The first param of ModifyStyle is
> dwRemove!!!)
> ELSE IF YOU DON'T WANT SORT THEN
> ADD SORT STYLE
>
>
> Do you see what's wrong? Here's the revised code, and it works on mine (I
> can send you the workspace and files if you want).
>
>
>

> CListBox * pLB = (CListBox*) GetDlgItem( IDC_LIST1 );
>
> /* Clear the list, change the style, and re-add the items
> * just to be sure sorting doesn't only work on adding.
> */
> pLB->ResetContent();
>
> if ( bSort )
> {

> pLB->ModifyStyle( 0, LBS_SORT );
> }
> else


> {
> pLB->ModifyStyle( LBS_SORT, 0 );
> }
>

> pLB-RedrawWindow();

0 new messages