Search code examples
c++mfccedit

Why CSpinButtonCtrl is not handling correctly numbers higher than 1000?


I have a dialog in MFC with a CSpinButtonCtrl and an attached buddy (CEdit). They work correctly when the maximum value of the spin control is lower than 1000, but if it is higher, the value in the CEdit is clamped to the thousand units when the the value is 1000 or higher (it is clamped to 4 instead of 4345, for example).

BEGIN
    EDITTEXT IDC_EDIT_1,274,42,40,14,ES_AUTOHSCROLL | ES_NUMBER
    CONTROL  "",IDC_SPIN_1,"msctls_updown32",UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_AUTOBUDDY | UDS_ARROWKEYS,313,42,11,14
END

The range is set programmatically:

const int max_value = 5000;
auto spin = (CSpinButtonCtrl*)GetDlgItem(IDC_SPIN_1);
spin->SetRange(1, max_value);

Any idea what's going on?


Solution

  • As I wasn't able to find any related question to it, I'm publishing my inquiries:

    The problem arose from the fact that when the CSpinButtonCtrl sets the text of the buddy it converts its numeric value into a string that, by default, includes the thousands separator. As it can be noticed, the CEdit control is set to accept only numbers. As the thousands separator is not a number, the CEdit clamps the text on it, leaving only the text at the left of the separator (the thousand units in my case).

    To solve it, just add the UDS_NOTHOUSANDS style to the spin control:

    CONTROL "",IDC_SPIN_1,"msctls_updown32",UDS_NOTHOUSANDS | UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_AUTOBUDDY | UDS_ARROWKEYS,313,42,11,14
    

    Of course, another option would be to remove the ES_NUMBER from the edit control, but that wasn’t my UI need.