1

试图让自定义对话框与按钮名称武器 1 、武器 2 和取消一起使用。但是使用此代码,当我尝试编译它时,它在 Result 上给出错误未定义错误消息是

[DCC 错误] ssClientHost.pas(760):E2003 未声明的标识符:“结果”

代码是:

      with CreateMessageDialog('Pick What Weapon', mtConfirmation,mbYesNoCancel) do
       try
           TButton(FindComponent('Yes')).Caption := Weapon1;
           TButton(FindComponent('No')).Caption := Weapon2;
           Position := poScreenCenter;
           Result := ShowModal;
        finally
     Free;
   end;
     if buttonSelected = mrYes    then ShowMessage('Weapon 1 pressed');
     if buttonSelected = mrAll    then ShowMessage('Weapon 2 pressed');
     if buttonSelected = mrCancel then ShowMessage('Cancel pressed');
4

2 回答 2

6

上面发布的代码有很多错误,除非您没有向我们展示某些部分。一方面,如果没有字符串变量Weapon1Weapon2,那么你不能引用这些变量!其次,如果没有Result变量(例如,如果代码在函数内部),那么这也是一个错误。此外,在上面的代码中,buttonSelected是一个变量,您可能也忘记声明了。最后,首先你谈论Yesand No,然后你谈论Yesand Yes to all

以下代码有效(独立):

with CreateMessageDialog('Please pick a weapon:', mtConfirmation, mbYesNoCancel) do
  try
    TButton(FindComponent('Yes')).Caption := 'Weapon1';
    TButton(FindComponent('No')).Caption := 'Weapon2';
    case ShowModal of
      mrYes: ShowMessage('Weapon 1 selected.');
      mrNo: ShowMessage('Weapon 2 selected.');
      mrCancel: ShowMessage('Cancel pressed.')
    end;
finally
  Free;
end;

免责声明:此答案的作者不喜欢武器。

于 2012-07-20T19:09:09.783 回答
1

结果仅在函数中定义:

function TMyObject.DoSomething: Boolean;
begin
  Result := True;
end;

procedure TMyObject.DoSomethingWrong;
begin
  Result := True;    // Error!
end;

所以,你会得到类似的东西:

function TMyForm.PickYourWeapon(const Weapon1, Weapon2: string): TModalResult;
begin
  with CreateMessageDialog('Pick What Weapon', mtConfirmation,mbYesNoCancel) do
    try
      TButton(FindComponent('Yes')).Caption := Weapon1;
      TButton(FindComponent('No')).Caption := Weapon2;
      Position := poScreenCenter;
      Result := ShowModal;
   finally
     Free;
   end;
   // Debug code?
{$IFDEF DEBUG)
   if Result = mrYes then 
     ShowMessage('Weapon 1 pressed');
   if Result = mrAll then 
     ShowMessage('Weapon 2 pressed');
   if Result = mrCancel then 
     ShowMessage('Cancel pressed');
{$ENDIF}
end;
于 2012-07-20T19:08:55.470 回答