DotNetRussell
DotNetRussell

Reputation: 9857

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

Upvotes: 2

Views: 8246

Answers (3)

NathanOliver
NathanOliver

Reputation: 180835

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

Upvotes: 6

PaulMcKenzie
PaulMcKenzie

Reputation: 35455

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.

Upvotes: 2

Jeff S
Jeff S

Reputation: 250

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.

Upvotes: 1

Related Questions