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

Recommended Answers

All 6 Replies

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

commented: Thank you its help my very much +1

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;
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;

Thank you very much.

mm, I suppose everybody forgot about 'MaxLength' property

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.