I noticed that when I set the MaxLength property of an Edit or DBEdit control, this does not stop me from putting text on the control larger than the MaxLength in code.
When I set the MaxLength = 12 for example, then
but I can do this :
Edit1.Text := '012345678901234567890123456789'
Is this normal behaviour ? Is this also so in Delphi Tokyo ?
And more important, is there an easy way to prevent this ?
It is the same in 10.2. Tokyo. The property TEdit.Text
is inherited unchanged from TControl
(through TCustomEdit
and TWinControl
). And TControl.SetText
does not know anything about MaxLength
, so it does not limit the text length. It indirectly issues a CM_TEXTCHANGED
message, but TCustomEdit
, which intercepts this, does not use that to limit the text length either.
The only way to prevent this is probably not to assign anything longer than 12 "characters", or to subclass TCustomEdit
into your own TMyEdit
, do the check in your own SetText
procedure, install the component and use that instead.