Delphi – Radio group, IF THEN ELSE and itemindex

Use the file below to show your students how the itemindex works in the radiogroup in Delphi. It also incorporates IF, THEN and ELSE.

screenshot_Sat_Jul_11_16.25.14

screenshot_Sat_Jul_11_16.19.36

procedure TForm1.btnGoClick(Sender: TObject);
begin
  if (rgpWeek.ItemIndex = 0) then
  lblDay.Caption := 'Monday'
  else if (rgpWeek.ItemIndex = 1) then
  lblDay.Caption := 'Tuesday'
  else if (rgpWeek.ItemIndex = 2) then
  lblDay.Caption := 'Wednesday'
  else if (rgpWeek.ItemIndex = 3) then
  lblDay.Caption := 'Thursday'
  else if (rgpWeek.ItemIndex = 4) then
  lblDay.Caption := 'Friday'
  else if (rgpWeek.ItemIndex = 5) then
  lblDay.Caption := 'Saturday'
  else if (rgpWeek.ItemIndex = 6) then
  lblDay.Caption := 'Sunday';
end;

You can also download the UNIT and PROJECT file below (or from the Resources folder in the IT section)

Download the Project/Unit files

 

2 Comments

  1. Hi, if I wanted to display what was selected in the RadioGroup, do I say Radiogroup.items.index….. or what?
    Much appreciated…

  2. case rgpWeek.ItemIndex of
    0: lblDay.Caption := ‘Monday’;
    1: lblDay.Caption := ‘Tuesday’;
    2: lblDay.Caption := ‘Wednesay’;
    3: lblDay.Caption := ‘Thursday’;
    4: lblDay.Caption := ‘Friday’;
    5: lblDay.Caption := ‘Saturday’;
    6: lblDay.Caption := ‘Sunday’;
    end;

    Would be a better solution rather than having all the if,else statements.

    @Kelsey,
    A little late, but better late than never. To display the value of a selected radiogroup you would use this.

    var
    selected: string;
    begin

    selected := radiogroup.items[radiogroup.itemindex];

    end;

    This is because the items are stored as a stringlist which is accessible like an array. 0 being the first line, 1 being the second and so forth.

Leave a Reply

Your email address will not be published.


*