Search code examples
delphieditbox

How do I display a label in an edit box, but switch to password-entry mode when it receives focus?


I use Delphi 10 and Windows 10.

The following code makes caret and selection disappear in Edit1.

procedure TForm1.Edit1Enter(Sender: TObject);
begin
  Edit1.PasswordChar := '*';
end;

After the focus moves to the other control and in onClick it works well.

I can't use onClick because the focus moves by tab key and the Edit1 should start with default #0 because it holds text which is 'password' before the focus enters.

How can I solve this?


Solution

  • The edit control works as designed and as expected.

    • If you want the control to hide a password then set then TEdit.PasswordChar in the OI or on creation or ... but not every time you enter the control
    • If you want to have a hint then set the TEdit.TextHint property which will be shown if TEdit.Text is empty and the control is not focused