iamsreesn
iamsreesn

Reputation: 21

Setting sort indicator on CListCtrl is clearing the column title

enter image description here

While setting the sort indicator on a particular column clearing that column's title.

Code Snippet

HDITEM headerInfo = {0};

for (int colindex = 0; colindex < n; colindex++)
{
    if (TRUE == pHDR->GetItem(colindex, &headerInfo))
    {

        headerInfo.mask = HDI_FORMAT;
    // column match?
        if (colindex == column)
        {
            if (ascending)
            {
                headerInfo.fmt |= HDF_SORTUP;
                headerInfo.fmt &= ~HDF_SORTDOWN;
            }
            else
            {
                headerInfo.fmt |= HDF_SORTDOWN;
                headerInfo.fmt &= ~HDF_SORTUP;
            }
        }
        // switch off sort arrows
        else
        {
            headerInfo.fmt &= ~HDF_SORTDOWN & ~HDF_SORTUP;
        }       
        pHDR->SetItem(colindex, &headerInfo);
    }
}

If I remove the headerInfo initialization its working fine in debug mode but its crashing in release mode. HDITEM headerInfo;

Column title was Name. It's cleared after clicking on it

Upvotes: 1

Views: 563

Answers (2)

iamsreesn
iamsreesn

Reputation: 21

At last I got the answer, we have to apply the HDI_FORMAT before GetItem. If we are applying after the GetItem, it will clear the masked flags. I tested and it is working as expected.

HDITEM headerInfo = {0};
headerInfo.mask = HDI_FORMAT;

for (int colindex = 0; colindex < n; colindex++)
{
if (TRUE == pHDR->GetItem(colindex, &headerInfo))
{
    // column match?
    if (colindex == column)
    {
        if (ascending)
        {
            headerInfo.fmt |= HDF_SORTUP;
            headerInfo.fmt &= ~HDF_SORTDOWN;
        }
        else
        {
            headerInfo.fmt |= HDF_SORTDOWN;
            headerInfo.fmt &= ~HDF_SORTUP;
        }
    }
    // switch off sort arrows
    else
    {
        headerInfo.fmt &= ~HDF_SORTDOWN & ~HDF_SORTUP;
    }       
    pHDR->SetItem(colindex, &headerInfo);
}
}

Thank you everyone for the suggestions and input.

Upvotes: 1

Vlad Feinstein
Vlad Feinstein

Reputation: 11311

You have a few things wrong.

First, if you call GetItem with headerInfo.mask set to 0 - you are not getting anything back. For the subsequent calls it will be set to HDI_FORMAT, so you will be getting format info.

Second, it is not clear what do you need that GetItem call anyway; you don't use any of the fields. You only test that this call returns TRUE, but that is expected for all valid column indexes.

And last, in your SetItem you are stating that the fmt member is valid, but it doesn't contain HDF_STRING flag, so no text will be displayed on that item.

Upvotes: 0

Related Questions