自定义对话框

时间:2012-07-20 18:58:05

标签: delphi

尝试使用自定义对话框来处理按钮名称weapon1,weapon2和cancel。但是,当我尝试编译时,使用此代码时,它会将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');

2 个答案:

答案 0 :(得分:6)

上面发布的代码有很多错误,除非您有部分内容未向我们展示。首先,如果没有字符串变量Weapon1Weapon2,那么你就不能引用这些变量了!其次,如果没有Result变量(例如,如果代码在函数内部),那么这也是一个错误。此外,在上面的代码中,buttonSelected是一个变量,您可能也忘记了它。最后,首先谈谈YesNo,然后谈谈YesYes 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;

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

答案 1 :(得分: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;