NumberEdit.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. return atof(csText);
  63. }
  64. BOOL CNumberEdit::SetNumber(long lNumber)
  65. {
  66. //Check if its a good number
  67. if(!ValidateNumber(lNumber))
  68. {
  69. MessageBeep(0);
  70. return FALSE;
  71. }
  72. //Its good
  73. CString csText;
  74. csText.Format("%d", lNumber);
  75. SetWindowText(csText);
  76. return TRUE;
  77. }