NumberEdit.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // NumberEdit.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "NumberEdit.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CNumberEdit
  13. CNumberEdit::CNumberEdit()
  14. {
  15. m_dMax = LONG_MAX;
  16. }
  17. CNumberEdit::~CNumberEdit()
  18. {
  19. }
  20. BEGIN_MESSAGE_MAP(CNumberEdit, CEdit)
  21. //{{AFX_MSG_MAP(CNumberEdit)
  22. ON_WM_CHAR()
  23. //}}AFX_MSG_MAP
  24. END_MESSAGE_MAP()
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CNumberEdit message handlers
  27. void CNumberEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  28. {
  29. //Only allow the number 0 - 9 and a the backspace to go through
  30. if(((nChar < '0') || (nChar > '9')) && (nChar != VK_BACK))
  31. return;
  32. CString csText;
  33. GetWindowText(csText);
  34. //Save this if the validate fails then these get set back
  35. int nStartChar, nEndChar;
  36. GetSel(nStartChar, nEndChar);
  37. //Set the new number
  38. CEdit::OnChar(nChar, nRepCnt, nFlags);
  39. //If its not valid set it back to the old number
  40. if(!ValidateNumber(GetNumberD()))
  41. {
  42. SetWindowText(csText);
  43. SetSel(nStartChar, nEndChar);
  44. }
  45. }
  46. BOOL CNumberEdit::ValidateNumber(double dNumber)
  47. {
  48. if(dNumber > m_dMax)
  49. return FALSE;
  50. return TRUE;
  51. }
  52. long CNumberEdit::GetNumber()
  53. {
  54. CString csText;
  55. GetWindowText(csText);
  56. return ATOL(csText);
  57. }
  58. double CNumberEdit::GetNumberD()
  59. {
  60. CString csText;
  61. GetWindowText(csText);
  62. #ifdef _UNICODE
  63. TCHAR *pEnd;
  64. double d = _tcstod(csText, &pEnd);
  65. #else
  66. double d = atof(csText);
  67. #endif
  68. return d;
  69. }
  70. BOOL CNumberEdit::SetNumber(long lNumber)
  71. {
  72. //Check if its a good number
  73. if(!ValidateNumber(lNumber))
  74. {
  75. MessageBeep(0);
  76. return FALSE;
  77. }
  78. //Its good
  79. CString csText;
  80. csText.Format(_T("%d"), lNumber);
  81. SetWindowText(csText);
  82. return TRUE;
  83. }