delphi – 如何在编辑框中隐藏密码文本?

前端之家收集整理的这篇文章主要介绍了delphi – 如何在编辑框中隐藏密码文本?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个输入框,并希望用户输入密码,但同时隐藏它.

这可能吗?

这是我的代码到目前为止

var password : string;
begin
 password := InputBox('Password: ','Please enter your password: ',password)
end;

解决方法

你不能’使用InputBox这样做,因为…很明显这个功能不会隐藏文本.

但标准的Windows编辑控件具有“密码模式”.要测试这个,只需将一个TEdit添加到窗体并将其PasswordChar设置为*.

如果你想在输入框中使用这样一个编辑,你必须自己写这个对话框,就像我的“超级输入对话框”一样:

type
  TMultiInputBox = class
  strict private
    class var
      frm: TForm;
      lbl: TLabel;
      edt: TEdit;
      btnOK,btnCancel: TButton;
      shp: TShape;
      FMin,FMax: integer;
      FTitle,FText: string;
    class procedure SetupDialog;
    class procedure ValidateInput(Sender: TObject);
  public
    class function TextInputBox(AOwner: TCustomForm; const ATitle,AText: string; var Value: string): boolean;
    class function NumInputBox(AOwner: TCustomForm; const ATitle,AText: string; AMin,AMax: integer; var Value: integer): boolean;
    class function PasswordInputBox(AOwner: TCustomForm; const ATitle,AText: string; var Value: string): boolean;
  end;

class procedure TMultiInputBox.SetupDialog;
begin
  frm.Caption := FTitle;
  frm.Width := 512;
  frm.Position := poOwnerFormCenter;
  frm.BorderStyle := bsDialog;
  lbl := TLabel.Create(frm);
  lbl.Parent := frm;
  lbl.Left := 8;
  lbl.Top := 8;
  lbl.Width := frm.ClientWidth - 16;
  lbl.Caption := FText;
  edt := TEdit.Create(frm);
  edt.Parent := frm;
  edt.Top := lbl.Top + lbl.Height + 8;
  edt.Left := 8;
  edt.Width := frm.ClientWidth - 16;
  btnOK := TButton.Create(frm);
  btnOK.Parent := frm;
  btnOK.Default := true;
  btnOK.Caption := 'OK';
  btnOK.ModalResult := mrOk;
  btnCancel := TButton.Create(frm);
  btnCancel.Parent := frm;
  btnCancel.Cancel := true;
  btnCancel.Caption := 'Cancel';
  btnCancel.ModalResult := mrCancel;
  btnCancel.Top := edt.Top + edt.Height + 16;
  btnCancel.Left := frm.ClientWidth - btnCancel.Width - 8;
  btnOK.Top := btnCancel.Top;
  btnOK.Left := btnCancel.Left - btnOK.Width - 4;
  frm.ClientHeight := btnOK.Top + btnOK.Height + 8;
  shp := TShape.Create(frm);
  shp.Parent := frm;
  shp.Brush.Color := clWhite;
  shp.Pen.Style := psClear;
  shp.Shape := stRectangle;
  shp.Align := alTop;
  shp.Height := btnOK.Top - 8;
  shp.SendToBack;
end;

class function TMultiInputBox.TextInputBox(AOwner: TCustomForm; const ATitle,AText: string; var Value: string): boolean;
begin
  FTitle := ATitle;
  FText := AText;

  frm := TForm.Create(AOwner);
  try
    SetupDialog;
    edt.NumbersOnly := false;
    edt.PasswordChar := #0;
    edt.Text := Value;
    edt.OnChange := nil;
    result := frm.ShowModal = mrOK;
    if result then Value := edt.Text;
  finally
    frm.Free;
  end;
end;

class function TMultiInputBox.PasswordInputBox(AOwner: TCustomForm;
  const ATitle,AText: string; var Value: string): boolean;
begin
  FTitle := ATitle;
  FText := AText;

  frm := TForm.Create(AOwner);
  try
    SetupDialog;
    edt.NumbersOnly := false;
    edt.PasswordChar := '*';
    edt.Text := Value;
    edt.OnChange := nil;
    result := frm.ShowModal = mrOK;
    if result then Value := edt.Text;
  finally
    frm.Free;
  end;
end;

class procedure TMultiInputBox.ValidateInput(Sender: TObject);
var
  n: integer;
begin
  btnOK.Enabled := TryStrToInt(edt.Text,n) and InRange(n,FMin,FMax);
end;

class function TMultiInputBox.NumInputBox(AOwner: TCustomForm; const ATitle,AMax: integer; var Value: integer): boolean;
begin
  FMin := AMin;
  FMax := AMax;
  FTitle := ATitle;
  FText := AText;

  frm := TForm.Create(AOwner);
  try
    SetupDialog;
    edt.NumbersOnly := true;
    edt.PasswordChar := #0;
    edt.Text := IntToStr(value);
    edt.OnChange := ValidateInput;
    result := frm.ShowModal = mrOK;
    if result then Value := StrToInt(edt.Text);
  finally
    frm.Free;
  end;
end;

尝试一下:

procedure TForm1.Button1Click(Sender: TObject);
var
  str: string;
begin
  str := '';
  if TMultiInputBox.PasswordInputBox(Self,'Password','Please enter your password:',str) then
    ShowMessageFmt('You entered %s.',[str]);
end;

Screenshot http://privat.rejbrand.se/superinputdlgpsw.png

原文链接:https://www.f2er.com/delphi/101526.html

猜你在找的Delphi相关文章