How to Disable a Tbuttongroup Item

Asked

Viewed 23 times

0

I was wondering if there’s a way enable / disable the items of a Tbuttongroup or at least add the property to it?

And I’ve tried it in the following ways.

 ButtonGroupRelatorios.Items.Items[2].Enabled := False; //Exception 000000
 ButtonGroupRelatorios.ItemIndex(0).Enabled := False; //Exception 000000
 TCustomControl(ButtonGroupRelatorios.Items.Items[2]).Enabled := False; //Exception 000000
 TButton(").Enabled := False; //Exception 000000
 TWinControl(").Enabled := False; //Exception 000000

When I put these lines presents this msg.

[dcc32 Error] UntPrincipal.pas(6617): E2003 Undeclared identifier: 'Enabled'
[dcc32 Error] UntPrincipal.pas(6619): E2066 Missing operator or semicolon

Have I tried searching on the website of http://docwiki.embarcadero.com/ but I couldn’t find anything to help me.

However I had to improvise using a Tscrollbox with Tbitbtn, it looks good but I can not change the color of Tbitbtn, while I wait for a solution I will use it.

  • Boy... I found a code on a forum that says solve this issue there... The site is delphiaccess.com i have not tested more I will leave the code as answer. to see if it helps you. Detail the site is in Spanish.

  • 1

    When it comes to unusual components the Embarcadero kind of fails to develop some useful properties.

1 answer

1

I followed the code I found on a forum on this subject. basically it creates a class it inherits from (Tgrpbuttonitem) and adds the property Enabled. I honestly did not test... nor did I know of the component success... but it is there. have fun.

unit Unit1;
 
interface
 
uses
  System.Classes,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.Controls,
  Vcl.ButtonGroup;
 
type
  TButtonGroup = class(Vcl.ButtonGroup.TButtonGroup)
  protected
    procedure DoItemClicked(const Index: Integer); override;
    function GetButtonClass: TGrpButtonItemClass; override;
  end;
 
  TGrpButtonItemEx = class(TGrpButtonItem)
  private
    FEnabled: Boolean;
    procedure SetEnabled(const Value: Boolean);
  public
    constructor Create(Collection: TCollection); override;
    property Enabled: Boolean read FEnabled write SetEnabled;
  end;
 
  TForm1 = class(TForm)
    ButtonGroup1: TButtonGroup;
    procedure FormCreate(Sender: TObject);
    procedure ItemClick(Sender: TObject);
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.ItemClick(Sender: TObject);
begin
  ShowMessage('ItemClick');
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  ButtonGroup1.ButtonOptions := [gboFullSize, gboShowCaptions];
  ButtonGroup1.Items[0].OnClick := ItemClick;
  ButtonGroup1.Items[1].OnClick := ItemClick;
  TGrpButtonItemEx(ButtonGroup1.Items[0]).Enabled := False;
end;
 
{ TGrpButtonItemEx }
 
constructor TGrpButtonItemEx.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  Enabled := True;
end;
 
procedure TGrpButtonItemEx.SetEnabled(const Value: Boolean);
begin
  FEnabled := Value;
end;
 
{ TButtonGroup }
 
procedure TButtonGroup.DoItemClicked(const Index: Integer);
var
  ButtonItem: TGrpButtonItem;
begin
  ButtonItem := Items[index];
  if Assigned(ButtonItem) and (ButtonItem is TGrpButtonItemEx) then
  begin
    if not TGrpButtonItemEx(ButtonItem).Enabled then Exit;
  end;
 
  inherited DoItemClicked(index);
end;
 
function TButtonGroup.GetButtonClass: TGrpButtonItemClass;
begin
  Result := TGrpButtonItemEx;
end;
 
end.

Scan it to see if you can adapt to what you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.