c++mfcccombobox

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

Solution

  • 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