Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Radiobuttons to RadioGroup at runtime 2

Status
Not open for further replies.

LucieLastic

Programmer
May 9, 2001
1,694
GB
hi all

I'm trying to add some radio buttons to a radiogroup at runtime but can't get it to work. I've tried the items.addobject because I need to store the id and explicitly creating the radio buttons but to no avail:

Code:
procedure TDataModMain.GetRadioButtons(radiogroup : TRadioGroup; sql : string);
  var Dataset : TADODataset;
      radio   : TRadioButton;
begin                //SQL must come in "select ID, Desc" order!!!
  Dataset := TADODataset.create(self);
  TRY
    radiogroup.items.clear;
    Globalaccess.RunSQL(_Database, sql, Dataset);

    while not Dataset.eof do
    begin
      radio := TRadioButton.create(self);
      radio.parent := radiogroup;
      radio.caption := Dataset.fields[1].asstring;
      radio.tag := Dataset.fields[0].asinteger;
//      radiogroup.items.addobject(Dataset.fields[1].asstring, pointer(Dataset.fields[0].asinteger));
      Application.processmessages;
      Dataset.next;
    end;

  FINALLY
    Dataset.close;
    FreeAndNil(Dataset);
  END;
end;
 
That might be a challenge because the buttons are stored in a list and are generated internally.

You could create a new class of TRadioGroup to expose some of the internal properties and methods to give you better access.

Another option could be to create a TPanel or TScrollBox and insert the radio buttons on to it.
 
I am probably being a bit simple here but I don't understand the problem. I dropped an empty TRadioGroup component and a TButton on a form. The following is the OnClick handler for the button which simply creates 4 radio buttons in the Radio Group.
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  radio: array [1..4] of TRadioButton;
  x: integer;
begin
  for x := Low(radio) to High(radio) do begin
    radio[x] := TRadioButton.Create( RadioGroup );
    RadioGroup.Items.AddObject( 'Test ' + IntToStr(x), radio[x] );
  end;
end;
And to show it works, I've implemnented an OnClick handler for the RadioGroup:
Code:
procedure TForm1.RadioGroupClick(Sender: TObject);
begin
  ShowMessage( RadioGroup.Items[RadioGroup.ItemIndex] );
end;
But maybe I'm missing the point of your original post?

Andrew
Hampshire, UK
 
hi Towerbase

Good answer but I was hoping to be able to store the IDs for each radio button in the tags but not sure it's possible, easily.

Thanks again
Lou
 
Derr! I've just realised that in your answer I should be able to set the tag. Thanks Towerbase.
 
Thanks Towerbase for your help, here is the full working version :eek:)

Code:
procedure TDataModMain.GetRadioButtons(RadioGroup : TRadioGroup; sql : string);
  var Dataset : TADODataset;
      ileft   : integer;
      Radio   : array of TRadioButton;
      x       : integer;
begin

  Dataset := TADODataset.create(self);
  TRY
    Globalaccess.RunSQL(_Database, sql, Dataset);  //SQL must come in "select ID, Desc" order!!!

    x := 0;
    SetLength(Radio, Dataset.recordcount);
    RadioGroup.Columns := Dataset.recordcount;
    while not Dataset.eof do
    begin
      radio[x]  := TRadioButton.Create(RadioGroup);
      radio[x].tag := Dataset.fields[0].asinteger;
      RadioGroup.Items.AddObject(Dataset.fields[1].asstring, radio[x]);

      Application.processmessages;
      Dataset.next;
    end;

  FINALLY
    SetLength(Radio, 0);
    FreeAndNil(Radio);
    Dataset.close;
    FreeAndNil(Dataset);
  END;
end;

function TDataModMain.GetIDFromRadios(RadioGroup : TRadioGroup) : integer;
begin
  result := TRadioButton(RadioGroup.items.Objects[RadioGroup.ItemIndex]).tag;
end;



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top