Mirror

Access TSpeedButtons in a TGroupBox (Views: 1)


Problem/Question/Abstract:

I need to put a lot of TSpeedButtons into a GroupBox (for example 20). For each button I set a GroupIndex. Is it possible to control what button was pressed without writing a SpeedButtonClick procedure for each button?

Answer:

The OnClick method passes the Sender in as a TObject. You can hook all the buttons up to the same OnClick methods and check to see which button was clicked something like this:

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  if Sender is TSpeedButton then
    with Sender as TSpeedButton do
    begin
      case GroupIndex of
        1: ;
        2: ;
        3: ;
      end;
    end;
end;

This is assuming the GroupIndex of each button is unique.

<< Back to main page