1

カスタムダイアログボックスをボタン名weapon1、weapon2で動作させ、キャンセルしようとしています。しかし、このコードでは、コンパイルしようとすると、結果が未定義としてエラーになります。エラーメッセージは次のとおりです。

[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は変数があり、これも宣言するのを忘れている可能性があります。最後に、最初にとについて話し、次にとについて話Yesします。NoYesYes 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 に答える