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;
1 Answer 1
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;
-
1\$\begingroup\$ Or
tbDeclaratie.Buttons[i].Down := (tbDeclaratie.Buttons[i].Name = LButtonName);
\$\endgroup\$Simon Forsberg– Simon Forsberg2015年10月17日 10:07:20 +00:00Commented 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\$Roman Marusyk– Roman Marusyk2015年10月19日 06:09:58 +00:00Commented Oct 19, 2015 at 6:09 -
\$\begingroup\$ yeah, unfortunately you'd still need an
if
for that. \$\endgroup\$Simon Forsberg– Simon Forsberg2015年10月19日 15:48:40 +00:00Commented 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\$Roman Marusyk– Roman Marusyk2015年10月19日 16:07:43 +00:00Commented Oct 19, 2015 at 16:07