2012-07-20 180 views
1

試圖讓自定義對話框使用按鈕名稱weapon1,weapon2和cancel。但與此代碼它給誤差上結果爲未定義當我嘗試編譯它 該錯誤消息自定義對話框

[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'); 
+0

'結果'未定義或'ShowModal'未定義? – 2012-07-20 19:06:29

+0

[DCC錯誤] ssClientHost.pas(760):E2003未聲明的標識符:'結果' – 2012-07-20 19:07:16

+4

然後,您不在功能中。結果是函數的結果,它在程序中沒有意義,所以它不可用。 – 2012-07-20 19:10:48

回答

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; 

免責聲明:本答案的作者是不喜歡的武器。

+9

我喜歡免責聲明;-)。 – 2012-07-20 19:13:48

+0

啊,得到你,謝謝你的幫助武器是在其他地方定義的,並使用案例而不是結果很好! ..它的武器的遊戲; D – 2012-07-20 19:16:49

+0

@Glen:我知道! :) – 2012-07-20 19:17:32

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; 
+0

現在我想到了,我知道:D ...但是這個代碼是作爲使用CreateMessageDialog的例子發佈的。我對這裏的某些事感到困惑。 – 2012-07-20 19:13:05

+0

我們都有我們的金髮時刻... – 2012-07-20 19:15:16