Marc Guillot
Marc Guillot

Reputation: 6465

How to have multiple TSpeedButtons down?

I'm trying to use several TSpeedButtons on a TFlowPanel to show a list of options that the user can choose. This is how I create those buttons:

  procedure FillOptions(ButtonPanel: TFlowPanel; Options: TStrings; Action: TNotifyEvent);
  var Option: string;
      Button: TSpeedButton;   
  begin
    for Option in Options do begin
      Button:= TSpeedButton.Create(ButtonPanel);
      Button.Caption := Option;
      Button.Width := Canvas.TextWidth(Option) + 20;
      Button.GroupIndex := 99;
      Button.AllowAllUp := True;
      Button.OnClick := Action;
      Button.Parent := ButtonPanel;
    end;
  end;

The problem is that I can't get to chose more than one option. When I click a button it goes down but the button previously chosen goes up.

What am I forgetting to set so more than one button can be down at the same time ?.

Thank you.

Upvotes: 1

Views: 317

Answers (1)

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109158

The documenatation on TSpeedButton.GroupIndex fully explains this (emphasis mine):

When GroupIndex is 0, the button behaves independently of all other buttons on the form. When the user clicks such a speed button, the button appears pressed (in its clicked state) and then returns to its normal up state when the user releases the mouse button.

When GroupIndex is greater than 0, the button remains selected (in its down state) when clicked by the user. When the user clicks a selected button, it returns to the up state, unless AllowAllUp is false. Setting the GroupIndex property of a single speed button to a value greater than 0 causes the button to behave as a two-state button when AllowAllUp is true.

Speed buttons with the same GroupIndex property value (other than 0) work together as a group. When the user clicks one of these buttons, it remains selected until the user clicks another speed button belonging to the same group. Speed buttons used in this way can present mutually exclusive choices to the user.

In other words, this is how speed buttons work. If five speed buttons belong to the same group, at most one of them can be selected (down) at any given point in time.

The AllowAllUp property merely makes it possible to have zero buttons selected (down); there is no way to have more than one down at the same time, unless they have different values of GroupIndex.

So, you need to give each button its own non-zero GroupIndex and also make sure that they all have AllowAllUp = True. Then each button will toggle individually.

Upvotes: 5

Related Questions