Scientific Notation and Leading Zeros in Spin Editor
Environment
Product Version | 2018.1 220 |
Product | RadSpinEditor for WinForms |
Description
RadSpinEditor is working with numeric values. A common requirement is to display the number in the editor in a specific format. This article will demonstrate how a scientific notation and leading zeros format can be implemented.
Solution
The control supports Hexadecimal format out of the box. The default implementation will be extended to support the new scientific and leading zeros formats which will be working similarly as the built-in Hexadecimal implementation.
The animation below demonstrates the end result.
Figure 1: Custom Formats
The custom functionality will be achieved with the help of special RadSpinEditor and RadSpinEditorElement classes.
1. Create a custom control extending the RadSpinEditor class.
- Add a new ScientificNatation property.
- Add a new LeadingZero property.
- Override the CreateSpinElement and return a custom type inheriting the RadSpinElement class.
Custom Spin Editor Control
2. Inherit the RadSpinElement class.
- Add a new ScientificNatation property.
- Add a new LeadingZero property.
Once set these new properties will force an update in our logic for extracting the formatted text from the value set to the control.
- Override the GetNumberText method. In this method we will format the value with scientific notation and leading zeros.
- Override the GetValueFromText method. This method is responsible to parse the custom formatted text to a decimal value.
- Override the SetSpinValue method. In this method we will force an update in the text according to the selected format.
Custom RadSpinElement Class
3. Build the project. The custom control should be displayed in the toolbox in Visual Studio. The newly introduced properties can be set straight from the designer.
A complete solution providing a C# and VB.NET project is available here.