PdnNumericUpDown.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace SmartCoalApplication
  10. {
  11. /// <summary>
  12. /// This class implements some common things on top of the regular NumericUpDown class.
  13. /// </summary>
  14. public class PdnNumericUpDown
  15. : NumericUpDown
  16. {
  17. public PdnNumericUpDown()
  18. {
  19. TextAlign = HorizontalAlignment.Right;
  20. }
  21. protected override void OnEnter(EventArgs e)
  22. {
  23. Select(0, Text.Length);
  24. base.OnEnter(e);
  25. }
  26. protected override void OnLeave(EventArgs e)
  27. {
  28. if (Value < Minimum)
  29. {
  30. Value = Minimum;
  31. }
  32. else if (Value > Maximum)
  33. {
  34. Value = Maximum;
  35. }
  36. decimal parsedValue;
  37. if (decimal.TryParse(Text, out parsedValue))
  38. {
  39. Value = parsedValue;
  40. }
  41. base.OnLeave(e);
  42. }
  43. }
  44. }