hi How to allow enter only 5 char in Tedit without using tmask?
Thanks.

Edited 6 Years Ago by especta: n/a

hi How to allow enter only 5 char in Tedit without using tmask?
Thanks.

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
  T:      string;
begin
  T := Edit1.Text;
  if (Length(T)>4) and (Key<>#8) then
    Key := #0;
end;

Key=#8 allows to be used BACKSPACE

Comments
Thank you its help my very much

it would be also good to check the length in case it is more than 5 each time, because if you paste some text (using shift-ins), it won't be filtered

In case about pasting some text:

procedure TForm1.Edit1Change(Sender: TObject);
var
  T:      string;
begin
  T := Edit1.Text;
  if (Length(T)>4) then
  begin
    Edit1.Text := Copy(T, 1, 5);
    Edit1.SelStart := 6;
  end;  
end;

Edited 6 Years Ago by finalist: n/a

procedure TForm1.Edit1Change(Sender: TObject);
var
  T:      string;
begin
  T := Edit1.Text;
  if (Length(T)>5) then
  begin
    Edit1.Text := Copy(T, 1, 5);
    Edit1.SelStart := 6;
  end;  
end;
This question has already been answered. Start a new discussion instead.