3

我正在编写一个程序,您必须在 InputBox 中输入密码才能访问程序的最小功能。但是,如果您在输入框中单击取消,我的程序会出现错误消息。所以我想知道是否有人知道我怎样才能做到这一点,因为使用 Messagedlg 我知道你使用 IF 。但是我怎样才能用 InputBox 把它弄好呢?

4

2 回答 2

11

InputBox()如果对话框被取消,则返回一个空白字符串,例如:

var
  Pass: String;

Pass := InputBox('Password needed', 'Enter the password:');
if Pass <> '' then
begin
  // use Pass as needed...
end;

或者,使用InputQuery()相反,它返回一个Boolean来指示对话框是否被取消,例如:

var
  Pass: String;

if InputQuery('Password needed', 'Enter the password:', Pass) then
begin
  // use Pass as needed...
end;
于 2013-05-30T07:52:08.650 回答
0

很多时候最好有一个自定义的 InputQuery

function InputValor(const aCaption: String; APrompt: string; var aValor: 
String): Boolean;
var
    vForm  : TForm;
    vLabel : TLabel;
    vBtnOk : TBitBtn;
    vValor : TEdit;
    vBtnCancel : TBitBtn;
begin
    Result  := False;
    vForm   := TForm.Create(Application);
    vLabel  := TLabel.Create(vForm);
    vValor  := TEdit.Create(vForm);
    vBtnOk  := TBitBtn.Create(vForm);
    vBtnCancel := TBitBtn.Create(vForm);

    with vForm do
    begin
        Name           := 'frmValor';
        Position       := poScreenCenter;
        BorderIcons    := [biSystemMenu];
        BorderStyle    := bsDialog;
        Caption        := aCaption;
        ClientHeight   := 150;
        ClientWidth    := 515;
        Color          := clBtnFace;
        OldCreateOrder := False;
        Font.Charset   := DEFAULT_CHARSET;
        Font.Color     := clWindowText;
        Font.Height    := -11;
        Font.Name      := 'Tahoma';
        Font.Style     := [];
        OldCreateOrder := False;
        PixelsPerInch  := 96;
        Left           := 0;
        Top            := 0;
    end;

    with vLabel do
    begin
        Name     := 'vLabel';
        Parent   := vForm;
        AutoSize := False;
        Left     := 18;
        Top      := 15;
        Width    := 484;
        Height   := 41;
        Caption  := APrompt;
        WordWrap := True;
    end;

    with vValor do
    begin
        Name      := 'vValorEdit';
        Parent    := vForm;
        Left      := 18;
        Top       := 62;
        Width     := 484;
        Height    := 21;
        Text      := '';
    end;

    with vBtnOk do
    begin
        Name        := 'vBtnOk';
        Parent      := vForm;
        Caption     := 'Aceptar';
        Left        := 335;
        Top         := 103;
        Width       := 75;
        Height      := 25;
        TabOrder    := 1;
        ModalResult := mrOk;
    end;

    with vBtnCancel do
    begin
        Name        := 'vBtnCancel';
        Parent      := vForm;
        Caption     := 'Cancelar';
        Left        := 427;
        Top         := 103;
        Width       := 75;
        Height      := 25;
        TabOrder    := 2;
        ModalResult := mrCancel;
    end;

    vForm.ShowModal;

    if (vForm.ModalResult = mrOk) and (vValor.Text <> '') then
    begin
        Result := True;
        aValor := vValor.Text;
    end else
    begin
        Result := False;
        aValor := '';
    end;

    FreeAndNil(vForm);
end;

使用方式与官方相同:

var
    vTest : String;
begin
    if (InputValor('Title', 'Label text', vTest) = True) then
        ShowMessage(vTest);
end;
于 2021-09-27T04:45:44.917 回答