Delphi 7 - 如何使用Inputbox

3

我正在编写一个程序,需要在InputBox中输入密码才能访问程序的主要功能。但是,如果您在InputBox上单击取消,我的程序会出现错误消息。因此,我想知道是否有人知道如何解决这个问题,因为我知道使用Messagedlg时需要使用IF语句。但是如何在InputBox中正确处理它呢?


1
请展示你的代码。 - Kromster
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;

@Remy_Lebeau 感谢您的帮助,我会去尝试一下。我还有一个问题,您知道如何制作一个掩码输入框吗?我知道需要更改什么,但是我需要编写一个新的InputBox函数吗? - 0x436f72647265
@CordreSmith 关于遮盖输入框的问题 https://dev59.com/cXRB5IYBdhLWcg3wiHz7 - bummi
将#31+'输入密码'作为第二个参数传递给InputQuery会导致文本框中的TEdit回显密码字符而不是输入的文本。 - Freddie bell
@Freddiebell:任何小于#32的值都会导致TEdit.PasswordChar被设置为'*'。然而,该功能是在XE2中引入的,因此在D7中不存在。 - Remy Lebeau

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接