function RadioButtonDialog(ACaption, Alabel: String; const AItems: array of string;
var AItemIndex: Integer): Boolean;
var
F: TForm;
rg: TRadioGroup;
i: Integer;
cancelBtn, okBtn: TButton;
w, h: Integer;
begin
F := TForm.Create(nil);
try
F.Caption := ACaption;
// Create the radiogroup
rg := TRadioGroup.Create(F);
rg.Parent := F;
// Set caption of radiogroup
rg.Caption := ALabel;
// Create items of radiogroup
for i:=0 to High(AItems) do
rg.Items.Add(AItems[i]);
// Select the predefined item
rg.ItemIndex := AItemIndex;
// Automatically determine size of radiogroup
rg.BorderSpacing.Around := 8;
rg.AutoSize := true;
rg.HandleNeeded;
rg.GetPreferredSize(w, h);
rg.AutoSize := false;
rg.Align := alTop;
rg.Height := h;
// Create the OK button
okBtn := TButton.Create(F);
okBtn.Parent := F;
okBtn.Caption := 'OK';
okBtn.ModalResult := mrOK;
okBtn.Default := true;
// Create the Cancel button
cancelBtn := TButton.Create(F);
cancelBtn.parent := F;
cancelBtn.BorderSpacing.Around := 8;
cancelBtn.Caption := 'Cancel';
cancelBtn.Cancel := true;
cancelBtn.ModalResult := mrCancel;
// Automatically position the cancel button underneath the radiogroup and
// anchor it to the right of the form
cancelBtn.AnchorSideTop.Control := rg;
cancelBtn.AnchorSideTop.Side := asrBottom;
cancelBtn.Anchors := [akTop, akRight];
cancelBtn.AnchorSideRight.Control := F;
cancelBtn.AnchorSideRight.Side := asrBottom;
// Automatically position the OK button to the left of the cancel button
okBtn.BorderSpacing.Around := 8;
okbtn.AnchorSideTop.Control := rg;
okbtn.AnchorSideTop.Side := asrBottom;
okbtn.Anchors := [akTop, akRight];
okbtn.AnchorSideRight.Control := cancelbtn;
okbtn.AnchorSideRight.Side := asrLeft;
// Automatically determine best size of the form
F.AutoSize := true;
// Position the form in the screen center
F.Position := poScreenCenter;
// Show form modally
if F.ShowModal = mrOK then begin
Result := true;
AItemIndex := rg.ItemIndex;
end else
Result := false;
finally
F.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
sel: Integer;
begin
sel := 0;
if RadioButtonDialog(
'Please select',
'Options',
['1st item', '2nd item', '3rd item', '4th item - very, very, very long text',
'5th item', '6th item', '7th item'],
sel)
then
ShowMessage('Item ' + IntToStr(sel) + ' selected');
end;