Just like with any other control, you can do that by toggling the Enabled property.
TRadioGroup has the Buttons property (https://docwiki.embarcadero.com/Libraries/Sydney/en/Vcl.ExtCtrls.TCustomRadioGroup.Buttons), which gives access to controls. The list and control names are defined by the Items property (a list of strings), so by going through Buttons via Items.Count, you can find the needed RadioButton.
Another universal way is by Controls (https://docwiki.embarcadero.com/Libraries/Sydney/en/Vcl.Controls.TWinControl.Controls) using ControlCount https://docwiki.embarcadero.com/Libraries/Sydney/en/Vcl.Controls.TWinControl.Controls, but in that case, each TControl needs to be checked whether it’s a TRadioButton. This works if TRadioButtons are used on a TPanel/TRadioGroup, or any other container.
But how to distinguish one TRadioButton from another?
That’s where the Tag property comes in. It should be used to uniquely identify each item so that you can manipulate them and keep localization flexible (since beginners often try to find elements by Caption, which is not reliable if text changes).
If you need to manage the list items created at runtime, you should assign a unique Tag during creation.
type
TForm1 = class(TForm)
RadioGroup1: TRadioGroup;
btnTurnOffTwo: TButton;
procedure FormCreate(Sender: TObject);
procedure btnTurnOffTwoClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnTurnOffTwoClick(Sender: TObject);
begin
for var i := 0 to RadioGroup1.Items.Count - 1 do
begin
var
LItem := RadioGroup1.Buttons[i];
if LItem.tag = 2 then // turn off radio buton "Two"
begin
LItem.Checked := False;
// check it off to avoid invalid write value to somewhere, if it disabled then can't be checked by user.
LItem.Enabled := False; // turn of setting that to False
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Fill radiogroup with controls. A Tag might be used to store unique ID of each element for further individual work witch each element
RadioGroup1.Items.Add('One');
RadioGroup1.Buttons[Pred(RadioGroup1.Items.Count)].Tag := 1;
RadioGroup1.Items.Add('Two');
RadioGroup1.Buttons[Pred(RadioGroup1.Items.Count)].Tag := 2;
RadioGroup1.Items.Add('Three disabled by default');
RadioGroup1.Buttons[Pred(RadioGroup1.Items.Count)].Tag := 3;
RadioGroup1.Buttons[Pred(RadioGroup1.Items.Count)].Enabled := False;
end;
Startup:
After pressing a button:
Answered by developers at Softacom, experts in Delphi modernization and legacy migration.