CGXControl::OnValidate

virtual BOOL OnValidate();

Return Value

TRUE if the cell’s data are valid; FALSE otherwise.

Remarks

This method is called when the user has confirmed the cell contents. The base-class version of this method calls CGXGridCore::OnValidateCell.

You can either override CGXGridCore:: OnValidateCell for all controls or subclass a specific control and override the control’s OnValidateCell method.

You have two options for displaying a message box with warning text when the data are invalid:

a) Call SetWarningText and specify the text. The grid component will later call the overridable method DisplayWarningText to display the message box.

b) Call AfxMessageBox directly from within OnValidateCell,which lets you gather user feedback, as for example you may ask the user if he wants to ignore the data or accept it. Note: When you want to display a message box yourself, you have to follow some rules (See the attached example. Most important is to call m_bWarningTextDisplayed = TRUE).

The base-class version of this method will also call ValidateString to validate the cell’s data.

Example

This example show you how you can override OnValidate and call SetWarningText:

BOOL CMyControl::OnValidate()
{
   if (m_nRow && m_nCol)
   {
      CString sText;
      GetCurrentText(sText);
      if (!sText.IsEmpty() && atol(sText) > 100)
      {
         Grid()->SetWarningText("Please enter a value between 0 and 100!");
         return FALSE;
      }
   }
   return Grid()->OnValidateCell(m_nRow, m_nCol);
}

The second example displays a message box and also demonstrates how to correct the value or reset the cell to its previous value. Please note that we check the m_bLockActivateGrid before we display a message box. This prevents a message box from being displayed when the grid loses focus because a user hit the “Cancel” key in a dialog.

BOOL CMyControl::OnValidate()
{
   CString s;
   // retrieve text from current cell
   GetCurrentText(s);
   if (_ttol(s) < 0 || _ttol(s) > 100)
   {
      TCHAR sz[255];
      wsprintf(sz, _T("Please enter a value between 0 and 100!"));
      Grid->ScrollCellInView(m_nRow, m_nCol);
      // Display message box
      if (Grid()->m_bLockActivateGrid)
      {
         AfxMessageBox(sz);
         // m_bWarningTextDisplayed is looked up in OnLButtonHitRowCol
         // if you set this TRUE, OnLButtonHitRowCol will cancel any further mouse processing
         Grid()->m_bWarningTextDisplayed = TRUE;
         // If you want the current cell to be undone, call
         // Grid()->TransferCurrentCell(FALSE);
         // This is only a sample. You might also display a message box
         // and ask the user if he wants to retry or ignore the value or
         // you could also call SetCurrentText() to change
         // the value.
         // SetCurrentText("22");
      }
      // else do nothing, the focus will go back to the current cell
      return FALSE;
   }
   return TRUE;
}

See Also

 CGXGridCore::OnValidateCell  CGXGridCore::SetWarningText  CGXControl::GetCurrentText  CGXControl::ValidateString

CGXControl

 Class Overview |  Class Members