1

This is in Delphi Pascal

I have dynamical created a 2d array of buttons. I want it such that when you click these buttons, the button pressed will dissapear. (This is to make a minesweeper style game).

Currently i have this array of these buttons, and am trying to set the onclick event to a procedure and passing in the x and y indexes of this button to the procedure such that it can hide the correct button.

Here is what i am using to link to the procedure: buttons[x,y].OnClick := buttonClicked(x,y);

The x and y are made from a for loop, in order that i can have a 2d array.

An this is the error I am getting.

E2010 Incompatible types: 'TNotifyEvent' and 'procedure, untyped pointer or untyped parameter'

Here is the Procedure:

procedure TMainPage.buttonClicked(buttonx: Integer; buttony: Integer);
begin
   buttons[buttonx,buttony].Visible:=False;
end;

Im assuming that the problem is that you cannot send variables through on the OnClick event. I am unsure of a way to do what i want, so guidance would be appreciated.

1
  • What the E2010 is telling you is that buttonClicked needs to be a method of an object, not a stand-alone procedure and have the same method signature as an OnClick handler, otherwise they are not assignment compatible. If you don't know these concepts, you have some reading to do. Commented Mar 19, 2019 at 19:09

2 Answers 2

6

Currently i have this array of these buttons, and am trying to set the onclick event to a procedure and passing in the x and y indexes of this button to the procedure such that it can hide the correct button.

You will not be able to pass that information directly to the OnClick event handler using input parameters. The event is declared as a TNotifyEvent, so the only input parameter it can accept is a TObject for the object that triggers the event.

However, that object is the clicked TButton itself, so you can simply use it as-is:

procedure TMainPage.buttonClicked(Sender: TObject);
begin
  TButton(Sender).Visible := False;
end;

If, for some reason, you actually needed the XY indexes within the array, you would have to pass those values to the event handler indirectly, such as with the button's Tag property:

buttons[x,y].Tag := Integer(SmallPoint(x, y));

...

procedure TMainPage.buttonClicked(Sender: TObject);
var
  pt: TSmallPoint;
begin
  pt := TSmallPoint(TButton(Sender).Tag);
  // use pt.x and pt.y as needed...
end;

Though, a safer option would be to just derive a new class from TButton instead, and then you can add whatever you want to it:

type
  TMyButton = class(TButton)
  public
    ArrayX, ArrayY: Integer;
  end;

...

buttons[x,y] := TMyButton.Create(...);
buttons[x,y].ArrayX := x;
buttons[x,y].ArrayY := y;

...

procedure TMainPage.buttonClicked(Sender: TObject);
var
  Btn: TMyButton;
begin
  Btn := TMyButton(Sender);
  // use Btn.ArrayX and Btn.ArrayY as needed...
end;
Sign up to request clarification or add additional context in comments.

1 Comment

I think you should add a reference to TNotifyEvent as it gives TButton OnClick As an Example
0

As Remy pointed out, there's a simpler way to have the button be made not visible.

To set the OnClick event when creating each new button, use the following:

buttons[x,y].OnClick := @buttonClicked;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.