Gentlefolk,
Environment: Linux Mint 18, Laz 1.84....
I have an object with a dynamic array of Buttons. Object, array size and buttons are created in code, all works well.
The 'OnClick' event handler for each button is set to @AllButtonClick.
This 'AllButtonClick' common code uses the 'AllButtonClick(Sender: TObject) tButton(Sender).Tag to specify the 'clicked' button and change button color, etc.
The 'button' event handlers do not return anything so I cannot return what button was selected.
So I looked around for some sort 'callback' code that would be called by the the AllButtonClick event.
After some reading and fiddling I came up with the following pointer based code. It works but frightens me.....
Any help, directions, examples appreciated, Ian.
Process flow:
Create MyButtonHolder.
Execute MyButtonHolder.SetCall(@GetButtonNumber) << Saves A pointer to GetButtonNumber
Click on a button.
MyButtonHolder.AllButtonClick code is executed.
begin
..
..
FMBArray[Index].Color := clRed;
WriteLN(tfun(CallPtr));
end;
The code 'WriteLN(tfun(CallPtr))' is executed and imediatley after the code @GetButtonNumber is executed (from CallStack).
Strange and ugly:
The WriteLN is necessary, things do not compile otherwise.
In the 'CallBack' code tForm1.GetButtonNumber the 'Sender' returned is odd, of type tBrush.
My Button module code:
type
tFun = function() : String;
ptr = Pointer;
tMyButtonHolder = class
..
..
CallPtr : Pointer;
function SetCall(InPtr: Pointer): Boolean; // Stores the 'Callback pointer
function GetButton : Integer; // Returns clicked button index
end;
function tMButHolder.SetCall(InPtr: Pointer): Boolean;
begin
// InPtr = @TForm1.GetButtonNumber
CallPtr := InPtr;
end;
procedure tMButtonHolder.AllButtonClick(Sender: TObject);
// A click on all buttons winds up here, button is determined by Tag value
begin
..
..
FMBArray[Index].Color := clRed;
WriteLN(tfun(CallPtr));
end;
Main form code:
procedure tForm1.GetButtonNumber(Sender: TObject);
var
vButton : Integer;
Begin
..
vTheButton:= ButtonHolder.GetButton; // get the clicked button index
end;