Author: Tomas Rutkauskas
I am trying to write a custom action that will set the visible property of a
TStatusBar on and off. I assigned this action to a menu item and when I select this
menu item at runtime the status bar is hidden. The problem is that the menu item
(connected to the action) is disabled, so I can't view the statusbar again. I think
that it's a matter of how the TMenuActionLink behaves (the action controls the
enabled property of the menu ). I tried to set the enabled property in the action
to true , but no avail. The menu is still disabled. Is there any way to do this?
Answer:
I think that the best solution would be to write an action, which will have a
StatusBar property and, in case this property was assigned, set the statusbar's
visibility in the overridden Execute method. Here's an example:
1 { ... }2 TMyAction = class(TAction)
3 protected4 FStatusBar: TStatusBar;
5 procedure Notification(AComponent: TComponent; Operation: TOperation); override;
6 procedure SetStatusBar(AValue: TStatusBar);
7 public8 constructor Create(AOwner: TComponent); override;
9 function Execute: Boolean; override;
10 published11 property StatusBar: TStatusBar read FStatusBar write SetStatusBar;
12 end;
13 14 { ... }15 16 constructor TMyAction.Create(AOwner: TComponent);
17 begin18 inherited Create(AOwner);
19 DisableIfNoHandler := false;
20 FStatusBar := nil;
21 Caption := 'Turn On/ Off Status Bar';
22 end;
23 24 function TMyAction.Execute: Boolean;
25 begin26 Result := inherited Execute;
27 if Assigned(FStatusBar) then28 begin29 FStatusBar.Visible := not FStatusBar.Visible;
30 Checked := FStatusBar.Visible;
31 end;
32 end;
33 34 procedure TMyAction.Notification(AComponent: TComponent; Operation: TOperation);
35 begin36 inherited Notification(AComponent, Operation);
37 if (Operation = opRemove) and (AComponent = StatusBar) then38 StatusBar := nil;
39 end;
40 41 procedure TMyAction.SetStatusBar(AValue: TStatusBar);
42 begin43 if FStatusBar <> AValue then44 begin45 FStatusBar := AValue;
46 if Assigned(FStatusBar) then47 begin48 FStatusBar.FreeNotification(Self);
49 Checked := FStatusBar.Visible;
50 end51 else52 Checked := false;
53 end;
54 end;