Kevin Böhmer
Kevin Böhmer

Reputation: 456

TButtonItem's OnClick returns Sender of TCategoryButtons

I am currently trying out the TSplitView component in Delphi 10 Seattle. The structure looks like the following:

enter image description here

With the second TButtonCategory I am trying to create items programmatically with the following code:

procedure TMainF.DynamicMenuButtonClick(Sender: TObject);
begin
  if sender is TButtonItem then //false
    ShowMessage('Sender is TButtonItem'); 

  if sender is TCategoryButtons then //true
    ShowMessage('Sender is TCategoryButtons'); 
end;

procedure TMainF.FormCreate(Sender: TObject);
var
  i: integer;
begin
  for i:=0 to 10 do begin
    catMenuItems.Categories[1].Items[i] := TButtonItem.Create(catMenuItems.Categories[1].Items);
    catMenuItems.Categories[1].Items[i].Caption := 'Something';
    catMenuItems.Categories[1].Items[i].OnClick := DynamicMenuButtonClick;
  end;
end;

In the "DynamicMenuButtonClick" procedure I want to get information about what button was clicked, the problem is that the sender which is of type TCategoryButtons doesn't tell me that. Now I was wondering if I am just missing out on something or if this is indeed just not possible.

Upvotes: 1

Views: 787

Answers (2)

Uwe Raabe
Uwe Raabe

Reputation: 47704

TCategoryButtons provides an event OnButtonClicked which gives you the TButtonItem. Perhaps that might be useful here.

TCatButtonEvent = procedure(Sender: TObject; const Button: TButtonItem) of object;

Upvotes: 2

The problem is that the sender which is of type TCategoryButtons doesn't tell me that

Yes, it does. Use the SelectedItem property of the TCategoryButtons in your event handler.

procedure TForm1.DynamicMenuButtonClick(Sender: TObject);
var
    categoryButtons: TCategoryButtons;
begin
    categoryButtons := (Sender as TCategoryButtons);
    Memo1.Lines.Add(categoryButtons.SelectedItem.Caption);
end;

Upvotes: 3

Related Questions