Mirror

How to position the cursor on the right-hand side of a TEdit (Views: 100)


Problem/Question/Abstract:

I want to write an Edit component, but I want the cursor to stay on the right when the user types in new characters.

Answer:

procedure TForm1.Edit1Change(Sender: TObject);
begin
  if IsChanging then
    exit; {Avoid recursion}
  IsChanging := true;
  try
    {Remove the first character}
    Edit1.Text := copy(Edit1.Text, 2, length(Edit1.Text) - 1);
    {And move the cursor to the end of the text}
    Edit1.SelStart := length(Edit1.Text);
  finally
    IsChanging := false;
  end;
end;

At design time (or in Create), put several spaces as Edit1.Text. They will be replaced one by one with characters typed by the user. IsChanging is a private variable of type boolean.

<< Back to main page