我正在学习 Delphi,当我调用执行逻辑的私有函数时,无法显示对话框。它看起来像是一个空指针引用,但我找不到它在哪里。
以下是代码:
unit SandBox;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
AhojButton: TButton;
procedure AhojButtonClick(Sender: TObject);
private
procedure ShowDialog(amount: Integer);
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ShowDialog(amount: Integer);
var td: TTaskDialog;
var tb: TTaskDialogBaseButtonItem;
begin
try
td := TTaskDialog.Create(nil);
tb := TTaskDialogBaseButtonItem.Create(nil);
td.Caption := 'Warning';
td.Text := 'Continue or Close?';
td.MainIcon := tdiWarning;
td.CommonButtons := [];
tb := td.Buttons.Add;
tb.Caption := 'Continue';
tb.ModalResult := 100;
tb := td.Buttons.Add;
tb.Caption := 'Close';
tb.ModalResult := 101;
td.Execute;
if td.ModalResult = 100 then
ShowMessage('Continue')
else if td.ModalResult = 101 then
ShowMessage('Close');
finally
td.Free;
tb.Free;
end;
end;
procedure TForm1.AhojButtonClick(Sender: TObject);
begin
ShowDialog(100);
end;
end.
我试图实例化并释放TTaskDialog
和TTaskDialogBaseButtonItem
。
此行引发错误:
tb := TTaskDialogBaseButtonItem.Create(nil);