Click here to Skip to main content
Licence CPOL
First Posted 18 Aug 2001
Views 133,378
Bookmarked 40 times

CNumSpinCtrl - a simple numeric spin control

By | 11 Jul 2002 | Article
Numeric spin control for working with real numbers

Introduction

When I needed a numeric spin control, I searched both CodeGuru and CodeProject. Surprisingly I only found one article by T. VU KHAC on CodeGuru, but as it turned out his CNumSpinCtrl could only be used in pair with CNumEdit which was too restrictive for my purposes. So I ended up writing my own numeric spin control.

CNumSpinCtrl allows you to work with non-integer numbers quite easily. It provides methods to set up value range, increment, and current position. It also lets you format the output value either by specifying number of decimal places or by providing your own formatting string.

Usage

Using the control is not much different from using CSpinButtonCtrl. You would subclass a dialog item or create the control dynamically. Then all you need to do is to set up range and position and, if necessary, the output formatting. Here’s an example from the demo project:

BOOL CCNumSpinCtrlDemoDlg::OnInitDialog()
{
        CDialog::OnInitDialog();
        
        m_spinValue.SetDecimalPlaces (3);
        m_spinValue.SetTrimTrailingZeros (FALSE);
        m_spinValue.SetRangeAndDelta (0.1, 1.0, 0.05);
        m_spinValue.SetPos (0.5);
...

There one thing to watch out however - style. Make sure you uncheck the "Set buddy integer" style or, if you are creating control dynamically, do not add UDS_SETBUDDYINT style. Otherwise the Windows automatically resets buddy's value to some integer when user clicks up/down arrows.

Formatting

There are two ways to format the output value: by specifying a number of decimal places or by providing a formatting string. With the first method you will specify number of decimal places with a call to SetDecimalPlaces function. Passing -1 to this function will turn off rounding to decimal places (the value will be output with "%g" format string). You can also specify whether you want to trim any trailing zeros with a call to SetTrimTrailingZeros. With a second method you simply provide your own formatting string, which later will be used with CString’s Format function. Example: SetFormatString ("%.2e")

Remarks

Be sure to set appropriate formatting settings. If your formatting settings are inadequate for the current range and increment, the text in the buddy control may not change at all. For example, if you set the increment to be 0.005, but the number of decimal places only to 1, the value in buddy control will not change when a user clicks on up and down arrows. This is because the control does not keep the current value internally. The value is obtained from the buddy control before it is incremented or decremented. Then it is formatted and passed back to the buddy control. So the formatting settings are quite important.

Revisions

2002-07-03 Commented out ModifyStyle in InitControl - it didn't work, didn't remove UDS_SETBUDDYINT style. Put ASSERT instead. So now program will ASSERT if you forget to remove UDS_SETBUDDYINT style in resources.
Fixed wrapping. In some cases due to poor machine precision the wrapping condition didn't evaluate properly.
2002-06-05 Warren Stevens added wrapping ability. If the style of the spin control is set to "wrap", the value will wrap around if increased or decreased past the range limits.
2002-04-17 Fixed bug with trimming zeros. If number of decimal places was set to zero, it was still trimming zeros (e.g. 100 would become 1).
2001-08-24 Changed ON_NOTIFY_REFLECT to ON_NOTIFY_REFLECT_EX so that parent can also handle notification message from spin control. Thanks to Emmanuil Tsagarakis for this one.
2001-07-06 Original version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Damir Valiulin

Software Developer

Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGreat Job! PinmemberPaxmatic3:30 20 Jun '10  
GeneralConvert numbers to text PinsussAnonymous0:52 23 May '05  
GeneralRe: Convert numbers to text PinmemberROCKISDEAD0:54 23 May '05  
GeneralRe: Convert numbers to text PinmemberDamir Valiulin4:51 24 May '05  
QuestionHow about a draggable spin control? Pinmembereleroux8:30 27 Apr '05  
AnswerRe: How about a draggable spin control? PinmemberDamir Valiulin5:23 24 May '05  
GeneralRe: How about a draggable spin control? Pinmembereleroux6:44 24 May '05  
GeneralRe: How about a draggable spin control? Pinmembergizmocuz9:24 19 Nov '06  
GeneralFixes when buddy control is manually edited PinmemberHard Hat1:27 12 Oct '03  
GeneralRe: Fixes when buddy control is manually edited PinmemberDamir Valiulin8:38 14 Oct '03  
GeneralExcellent stuff.... Pinmemberade cooper5:40 9 Sep '03  
GeneralThe CNumSpinCtrl can't display on toolbar!!! PinsussChinaThink16:14 28 Aug '03  
GeneralRe: The CNumSpinCtrl can't display on toolbar!!! PinmemberDamir Valiulin4:10 2 Sep '03  
GeneralYou can do this with a regular CEdit and Spin control PinmemberRoger Allen4:20 23 Aug '02  
GeneralRe: You can do this with a regular CEdit and Spin control PinmemberDamir Valiulin4:31 23 Aug '02  
GeneralRe: You can do this with a regular CEdit and Spin control PinmemberRoger Allen4:46 23 Aug '02  
GeneralNice Job! Pinmemberjimetron5:35 29 Jul '02  
GeneralIt can't work in dialog bar ! Pinsusslijshu16:25 13 Jul '02  
GeneralRe: It can't work in dialog bar ! PinmemberDamir Valiulin3:29 15 Jul '02  
Generalstack overflow software exception Pinmembersuma557:54 3 Jul '02  
GeneralSmall bug found -> correction PinmemberNick Nougat9:29 13 Jun '02  
GeneralRe: Small bug found -> correction PinmemberDamir Valiulin8:31 3 Jul '02  
GeneralRe: Small bug found -> correction Pinmembertattalevieuxgrincheux11:29 16 May '03  
GeneralRe: Small bug found -> correction Pinmembertattalevieuxgrincheux11:30 16 May '03  
GeneralRe: Small bug found -> correction Pinmembertattalevieuxgrincheux11:30 16 May '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 12 Jul 2002
Article Copyright 2001 by Damir Valiulin
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid