<< Return to Main Index

< Return to Class Index

CGXControl::SetControlText

virtual BOOL SetControlText(ROWCOL nRow, ROWCOL nCol, const CString& strDisplay, UINT nFlags = GX_UPDATENOW, const CGXStyle* pOldStyle = NULL);

nRow

Specifies the row id of the cell.

nCol

Specifies the column id of the cell.

strDisplay

A reference to a string which holds the text to be stored in the cell.

nFlags

Specifies the update technique. See UpdateStyleRange for more information.

pOldStyle

An optional pointer to an existing style object for the cell. SetControlText can use this object to determine information about the cell (e.g., input mask or read-only state) without having to gather the style information from the grid.

Return Value

TRUE if the text could be assigned to the cell; FALSE if the text was invalid.

Remarks

Converts the display text and call SetValueRange with the value that should be stored in the grid. The actual text conversion is done by calling ConvertControlTextToValue.

The base-class version of this method will also ensure that the text length will not exceed the maximum length as specified with CGXStyle::SetMaxLength().

SetControlText will be called when you paste text from CF_TEXT clipboard format into the grid or load data from a text file. The default verison of SetControlText calls ConvertControlTextToValue to convert the text into value. You should use this function as a hook for adjusting the data.

CGXMaskControl has a special override of ConvertControlTextToValue and will remove any literals from the value before SetControlText stores the value via SetValueRange into the grid. CGXRichEditCtrl has overridden this method, too, and converts the unformatted text into an RTF string. CGXComboBoxWnd has a special mode where it searches the text in an associated choice list and only stores an index as value in the grid.

See ConvertControlTextToValue for an example.

Note

With version 6.0 we added the ConvertControlTextToValue value to give you the possibility to convert the display text into the grid value. ConvertControlTextToValue is simply a new hook in the base class version of SetControlText. Your existing SetControlText overrides will still work as before, but by overriding ConvertControlTextToValue you implement a more specialized function that is focused only on converting the text without having to call SetValueRange.

Example

This example shows you the implementation of CGXControl::SetControlText. The ConvertControlTextToValue override lets you adjust the value before it gets stored in the grid:

BOOL CGXControl::SetControlText(ROWCOL nRow, ROWCOL nCol, const CString& str, UINT nFlags, const CGXStyle* pOldStyle)
{
   BOOL bSuccess = FALSE;
   CGXStyle* pStyle = NULL;

   if (pOldStyle == NULL)
   {
      pStyle = Grid()->CreateStyle();
      Grid()->ComposeStyleRowCol(nRow, nCol, pStyle);
      pOldStyle = pStyle;
   }

   Grid()->m_pOldStyle = pOldStyle;

   CString sValue = str;
   bSuccess = ConvertControlTextToValue(sValue, nRow, nCol, pOldStyle);

   if (bSuccess)
   {
      int nMaxLength = 0;

      // check for read-only
      if (Grid()->GetParam()->IsLockReadOnly() &&
         (Grid()->IsReadOnly() || pOldStyle->GetIncludeReadOnly() && pOldStyle->GetReadOnly()))
         bSuccess = FALSE;

      // check for maximum length
      else if (pOldStyle->GetIncludeMaxLength()
         && (nMaxLength = (int) pOldStyle->GetMaxLength()) > 0
         && sValue.GetLength() > nMaxLength)
         bSuccess = Grid()->SetExpressionRowCol(nRow, nCol, sValue.Left(nMaxLength), nFlags);

      // default
      else
         bSuccess = Grid()->SetExpressionRowCol(nRow, nCol, sValue, nFlags);
   }

   if (pStyle)
      Grid()->RecycleStyle(pStyle);

   Grid()->m_pOldStyle = NULL;

   return bSuccess;
}

See Also

CGXControl::GetControlText CGXMaskControl CGXRichEditCtrl CGXComboBoxWnd CGXControl::ConvertControlTextToValue

CGXControl

Class Overview | Class Members