2

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);
}
user8128167
  • 6,929
  • 6
  • 66
  • 79
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • Look at those numbers carefully. They are sorted using the ASCII collating sequence, not numerically. If the combo box is sorted, it sorts via ASCII, not numbers. – PaulMcKenzie May 18 '15 at 16:25
  • 1
    No, I am sure it's sorting it. I'm not sure why @PaulMcKenzie – DotNetRussell May 18 '15 at 16:26
  • 1
    You have to take off the (I think it's called this) CBS_SORT flag from the combo box style. Then the numbers are added as you go along. – PaulMcKenzie May 18 '15 at 16:28

3 Answers3

5

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

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
2

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.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
1

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.

Jeff S
  • 250
  • 6
  • 13
  • The solution is to complex to open a designer. Need to do it in the resource file. Thanks! – DotNetRussell May 18 '15 at 16:32
  • 1
    @AnthonyRussell in that case, if you are in the resource file, just look for your combo box and remove CBS_SORT. Good Luck – Jeff S May 18 '15 at 16:41