Glen Morse
Glen Morse

Reputation: 2603

Custom Dialog Box

Trying to get a custom dialogbox to work with the button names weapon1 , weapon2, and cancel. But with this code it is giving error on Result as undefined when i try to compile it The error message is

[DCC Error] ssClientHost.pas(760): E2003 Undeclared identifier: 'Result'

The code is :

      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');

Upvotes: 1

Views: 5520

Answers (2)

Toon Krijthe
Toon Krijthe

Reputation: 53476

Result is only defined in a function:

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

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

So, you get something like:

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;

Upvotes: 1

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109158

The code posted above has a lot of errors, unless there are parts you are not showing us. For one thing, if there are no string variables Weapon1 and Weapon2, then you cannot refer to such variables! Second, if there is no Result variable (there is if the code is inside a function, for instance), then that's an error, too. Also, in your code above, buttonSelected is a variable, which you might have forgotten to declare as well. Finally, first you talk about Yes and No, then you talk about Yes and Yes to all.

The following code works (standalone):

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;

Disclaimer: The author of this answer is not fond of weapons.

Upvotes: 6

Related Questions