Microsoft KB Archive/169947: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - "&" to "&")
 
(3 intermediate revisions by the same user not shown)
Line 101: Line 101:
   void DDX_I64Txt( CDataExchange* pDX,
   void DDX_I64Txt( CDataExchange* pDX,
                     int            nIDC,
                     int            nIDC,
                     __int64&       nValue
                     __int64&      nValue
                   )
                   )
   {
   {
     TCHAR        szBuffer[32];
     TCHAR        szBuffer[32];
     HWND  hWndCtrl = pDX->PrepareEditCtrl(nIDC);
     HWND  hWndCtrl = pDX->PrepareEditCtrl(nIDC);


     if (pDX->m_bSaveAndValidate)
     if (pDX->m_bSaveAndValidate)
     {                                // _int64 is 19 characters long
     {                                // _int64 is 19 characters long
       *szBuffer = NULL;              // when converted to text.
       *szBuffer = NULL;              // when converted to text.
Line 127: Line 127:
   //  
   //  
   void DDV_MinMaxI64( CDataExchange* pDX,
   void DDV_MinMaxI64( CDataExchange* pDX,
                       __int64&       nValue,
                       __int64&      nValue,
                       __int64        nMinVal,
                       __int64        nMinVal,
                       __int64        nMaxVal
                       __int64        nMaxVal
                     )
                     )
   {
   {
     ASSERT( nMinVal <= nMaxVal);
     ASSERT( nMinVal <= nMaxVal);
     if( !pDX-&gt;m_bSaveAndValidate )
     if( !pDX->m_bSaveAndValidate )
     {
     {
   TRACE0(&quot;Warning: initial dialog data is out of range.\n&quot;);
   TRACE0("Warning: initial dialog data is out of range.\n");
   return;
   return;
     }
     }


     if( (nValue &lt; nMinVal) || (nValue &gt; nMaxVal) )
     if( (nValue < nMinVal) || (nValue > nMaxVal) )
     {
     {
       TCHAR  szMinStr[32];          // Have to use strings here because
       TCHAR  szMinStr[32];          // Have to use strings here because
Line 146: Line 146:
       _i64tot( nMinVal, szMinStr, 10 );
       _i64tot( nMinVal, szMinStr, 10 );
       _i64tot( nMaxVal, szMaxStr, 10 );
       _i64tot( nMaxVal, szMaxStr, 10 );
       _stprintf( szMsgStr, &quot;Enter a value between %s and %s\n&quot;,
       _stprintf( szMsgStr, "Enter a value between %s and %s\n",
                   szMinStr, szMaxStr );
                   szMinStr, szMaxStr );
       AfxMessageBox( szMsgStr, MB_ICONEXCLAMATION );
       AfxMessageBox( szMsgStr, MB_ICONEXCLAMATION );
                                     // Set the value to something defined
                                     // Set the value to something defined
       (nValue &lt; nMinVal) ? nValue = nMinVal : nValue = nMaxVal;
       (nValue < nMinVal) ? nValue = nMinVal : nValue = nMaxVal;


       pDX-&gt;Fail();                  // Restores focus to the
       pDX->Fail();                  // Restores focus to the
                                     // offending control and throws
                                     // offending control and throws
     }                              // a user exception.
     }                              // a user exception.

Latest revision as of 12:29, 21 July 2020

Knowledge Base


Article ID: 169947

Article Last Modified on 11/21/2006



APPLIES TO

  • Microsoft Foundation Class Library 4.2, when used with:
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 4.2 Professional Edition
    • Microsoft Visual C++ 5.0 Enterprise Edition
    • Microsoft Visual C++ 5.0 Professional Edition
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++ 6.0 Standard Edition



This article was previously published under Q169947

SUMMARY

DDX - DDV functions in MFC do not directly support the __int64 type. However, you can add your own functions to allow 64-bit numbers to be used with Edit controls.

MORE INFORMATION

Below is sample code for DDX - DDV routines to handle the __int64 type. This code can be easily modified to handle unsigned 64-bit numbers. This code uses the TCHAR datatype for text so it works with Unicode or standard character types without modification.

Sample Code

   // First we have to fix a bug in TCHAR.H.
   // _ttoi64 is incorrectly defined in TCHAR.H, so we need to undefine it
   // and redefine it correctly to avoid the compile error when building
   // and non-Unicode version.

   #ifdef _UNICODE
     #ifdef  _ttoi64
       #undef  _ttoi64
       #define _ttoi64 _wtoi64
     #endif
   #else
     #ifdef  _ttoi64
       #undef  _ttoi64
       #define _ttoi64 _atoi64
     #endif
   #endif


   //----------------------------------------------------------------------
   //  Use _ttoi64 and _i64tot to get the data into and out of the
   //  the member variables. These map to _atoi64 /_wtoi64 and
   //  _i64toa / _i64tow.
   // 
   void DDX_I64Txt( CDataExchange* pDX,
                    int            nIDC,
                    __int64&       nValue
                  )
   {
     TCHAR        szBuffer[32];
     HWND   hWndCtrl = pDX->PrepareEditCtrl(nIDC);

     if (pDX->m_bSaveAndValidate)
     {                                 // _int64 is 19 characters long
       *szBuffer = NULL;               // when converted to text.
       ::GetWindowText( hWndCtrl, szBuffer, 30 );
       nValue = _ttoi64( szBuffer );
     }
     else
     {
       _tcscpy( szBuffer, _i64tot( nValue, szBuffer, 10 ) );
       ::SetWindowText( hWndCtrl, szBuffer );
     }
   } // end DDX_I64Txt

                 //--------------------------------------------------------
------------
   //  Most DDV functions pass nValue by value. We pass a reference
   //  here so we can set the value to the min or max specified if
   //  an out-of-range value is passed in.
   // 
   void DDV_MinMaxI64( CDataExchange* pDX,
                       __int64&       nValue,
                       __int64        nMinVal,
                       __int64        nMaxVal
                     )
   {
     ASSERT( nMinVal <= nMaxVal);
     if( !pDX->m_bSaveAndValidate )
     {
   TRACE0("Warning: initial dialog data is out of range.\n");
   return;
     }

     if( (nValue < nMinVal) || (nValue > nMaxVal) )
     {
       TCHAR  szMinStr[32];          // Have to use strings here because
       TCHAR  szMaxStr[32];          // CString and sprintf formatting
       TCHAR  szMsgStr[64];          // don't support 64-bit numbers.
       _i64tot( nMinVal, szMinStr, 10 );
       _i64tot( nMaxVal, szMaxStr, 10 );
       _stprintf( szMsgStr, "Enter a value between %s and %s\n",
                  szMinStr, szMaxStr );
       AfxMessageBox( szMsgStr, MB_ICONEXCLAMATION );
                                     // Set the value to something defined
       (nValue < nMinVal) ? nValue = nMinVal : nValue = nMaxVal;

       pDX->Fail();                  // Restores focus to the
                                     // offending control and throws
     }                               // a user exception.
   } // End DDV_MinMaxI64.
                

Keywords: kbhowto kbuidesign KB169947