2
\$\begingroup\$

I have an action on four buttons. I need to make a down (checked) button if it's pressed and other buttons up (unchecked). My button is TToolButton and the property Style is set to tbsCheck.

Could you please suggest improvements?

LButtonName := (Sender as TAction).ActionComponent.Name;
   if LButtonName.Equals('btnVorigeWeek') then
      begin
        LSelectionType := 2;
        btnHuidigeWeek.Down := False;
        btnVorigeWeek.Down := True;
        btnHuidigeMaand.Down := False;
        btnVorigeMaand.Down := False;
      end
      else if LButtonName.Equals('btnHuidigeMaand') then
      begin
        LSelectionType := 3;
        btnHuidigeWeek.Down := False;
        btnVorigeWeek.Down := False;
        btnHuidigeMaand.Down := True;
        btnVorigeMaand.Down := False;
      end
      else if LButtonName.Equals('btnVorigeMaand') then
      begin
        LSelectionType := 4;
        btnHuidigeWeek.Down := False;
        btnVorigeWeek.Down := False;
        btnHuidigeMaand.Down := False;
        btnVorigeMaand.Down := True;
      end
      else
      begin
        LSelectionType := 1;
        btnHuidigeWeek.Down := True;
        btnVorigeWeek.Down := False;
        btnHuidigeMaand.Down := False;
        btnVorigeMaand.Down := False;
      end;
\$\endgroup\$
1
\$\begingroup\$

It seems I found better solution. All these buttons are located on ToolBar.

I can loop through the ToolBar and utilize the Name property on the buttons to compare with. So, I've changed it to:

  var
    LSelectionType, i: Integer;
    LButtonName: string;
  begin
    LSelectionType := 1;
    LButtonName := (Sender as TAction).ActionComponent.Name;    
    for i := 0 to ToolBar.ButtonCount -1 do
    begin
      ToolBar.Buttons[i].Down := (ToolBar.Buttons[i].Name = LButtonName);
      if ToolBar.Buttons[i].Down then
        LSelectionType := i + 1;
    end;
    ...
  end;

UPDATE

Finally, I set proper Tag property and set property Grouped to True for buttons and changed code:

  var
    LSelectionType: Integer;
    LActionComponent: TComponent;
  begin
    LSelectionType := 1;
    LActionComponent := (Sender as TAction).ActionComponent;

    if LActionComponent is TToolButton then
    begin
      (LActionComponent as TToolButton).Down := True;
      LSelectionType := (LActionComponent as TToolButton).Tag;
    end;
    // and using LSelectionType 
    ....
  end;
\$\endgroup\$
  • 1
    \$\begingroup\$ Or tbDeclaratie.Buttons[i].Down := (tbDeclaratie.Buttons[i].Name = LButtonName); \$\endgroup\$ – Simon Forsberg Oct 17 '15 at 10:07
  • \$\begingroup\$ @SimonForsberg Thanks. I like your solution. But is this case what to do with LSelectionType := i + 1;? \$\endgroup\$ – Roman Marusyk Oct 19 '15 at 6:09
  • \$\begingroup\$ yeah, unfortunately you'd still need an if for that. \$\endgroup\$ – Simon Forsberg Oct 19 '15 at 15:48
  • \$\begingroup\$ @SimonForsberg. Ok, it is not a problem. I used your advice and add if. Looks fine for me. Thanks. \$\endgroup\$ – Roman Marusyk Oct 19 '15 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.