Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CEdit::LimitText

Call this function to limit the length of the text that the user may enter into an edit control.

void LimitText( 
   int nChars = 0  
);
nChars

Specifies the length (in bytes) of the text that the user can enter. If this parameter is 0, the text length is set to UINT_MAX bytes. This is the default behavior.

Changing the text limit restricts only the text the user can enter. It has no effect on any text already in the edit control, nor does it affect the length of the text copied to the edit control by the SetWindowText member function in CWnd. If an application uses the SetWindowText function to place more text into an edit control than is specified in the call to LimitText, the user can delete any of the text within the edit control. However, the text limit will prevent the user from replacing the existing text with new text, unless deleting the current selection causes the text to fall below the text limit.

NoteNote:

In Win32 (Windows NT and Windows 95/98), SetLimitText replaces this function.

For more information, see EM_LIMITTEXT in the Windows SDK.

// Limit the number of characters to be the maximum number visible. 

// Get the text metrics for the edit; needed for the 
// average character width.
TEXTMETRIC tm;
CDC* pDC = m_myEdit.GetDC();
pDC->GetTextMetrics(&tm);
m_myEdit.ReleaseDC(pDC);

CRect r;
m_myEdit.GetRect(&r);
m_myEdit.LimitText(r.Width()/tm.tmAveCharWidth);

Header: afxwin.h

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.