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;
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 16, 2015 at 22:42
\$\endgroup\$

1 Answer 1

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;
answered Oct 16, 2015 at 23:29
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Or tbDeclaratie.Buttons[i].Down := (tbDeclaratie.Buttons[i].Name = LButtonName); \$\endgroup\$ Commented Oct 17, 2015 at 10:07
  • \$\begingroup\$ @SimonForsberg Thanks. I like your solution. But is this case what to do with LSelectionType := i + 1;? \$\endgroup\$ Commented Oct 19, 2015 at 6:09
  • \$\begingroup\$ yeah, unfortunately you'd still need an if for that. \$\endgroup\$ Commented Oct 19, 2015 at 15:48
  • \$\begingroup\$ @SimonForsberg. Ok, it is not a problem. I used your advice and add if. Looks fine for me. Thanks. \$\endgroup\$ Commented Oct 19, 2015 at 16:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.