简体   繁体   中英

CComboBox Sorting

So I am attempting to cut my CPP teeth on an existing application.

I ran into a bit of a snag. My combobox items are being added in order as you can see below. However, the output is

[1,10,11,12,13,14,15,2,3,4,5,6,7,8,9]

I have looked at the CComboBox documentation here . Yet, I still am confused as to why this is producing this result.

for (int i = 1; i <= m_pPage2->GetNumberColumns(); i++)
{
    CString szColNum;
    szColNum.Format (_T("%d"), i);
    m_cSubColumn.AddString(szColNum);
}

The standard comparison functions don't deal well with strings containing numbers. They do not take into account that the size of the string should also come into play. With that since "10" starts with a "1" then it will come before anything that has more than a "1" at index 0.

If you were to pad all of your number with leading zeros so that the string sizes were the same it would sort it in the normal numerical order.

To stop the CComboBox from sorting its contents when you use AddString() you need to set the CBS_SORT property to false

The issue is that your combo box is using the CBS_SORT style, thus the data is sorted using the ASCII collating sequence.

To turn off the sorting, you have to remove the CBS_SORT style from the combo box. Depending on the resource tool(s) you use, this style can be removed by checking some item in your tool to turn on/off sorting, or go straight to the resource file itself and just remove the CBS_SORT style from the combo box definition.

In the Properties window for the Combo Box make sure set is set to False. This will allow your combo box to display the data the way you have it input.

*This is what I do in Visual Studio, I didn't see where you said what IDE you were using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM