radiogroup can not be made active lazarus / 31-LCL ---------------------------------------------------------------------- hy · 2014-11-05 16:06 I want to make a simple dialog window with several choices = items in a radiogroup. For better usability I want the radiogroup already to be focussed when the window is shown. if I use (in onCreate) Code: [Select]activecontrol := rg; //radiogroupIt doesn't work If I use (in FormActivate) Code: [Select]if rg.canfocus then       rg.SetFocus; it doesn't work either. ---------------------------------------------------------------------- howardpc · 2014-11-05 17:36 Yes, focusing a TRadioGroup is not straightforward. I think you have to do something like this (where your radiogroup is named rg): Code: [Select]procedure TForm1.FormActivate(Sender: TObject); var   i: integer;   rb: TRadioButton; begin   i := rg.ItemIndex;   if (i > -1) then begin     rb:=(FindComponent(Format('RadioButton%d',[i])) as TRadioButton);     if (rb <> nil) then       rb.SetFocus;   end; end; ---------------------------------------------------------------------- hy · 2014-11-05 21:24 Thanks howardpc. To make it work I had to change the code to: Code: [Select]if (rg.ItemIndex > -1) and (rg.Items.Count > 0)then       for i:=0 to rg.ControlCount-1 do          if (rg.Controls[i] is TRadioButton) then          begin             TRadioButton(rg.Controls[i]).SetFocus;             Exit;          end; And I could simplify it to: Code: [Select]if (rg.ItemIndex > -1) and (rg.Items.Count > 0)then       TRadioButton(rg.Controls[rg.ItemIndex]).SetFocus; But this works only if itemindex = 0 here. It doesn't work because there is a hidden radiobutton added - you can check it out with the following code: Code: [Select]if (rg.ItemIndex > -1) and (rg.Items.Count > 0)then    begin       for i:= 0 to rg.ControlCount-1 do       begin          //TRadioButton(rg.Controls[rg.ItemIndex]).SetFocus;          s := TRadioButton(rg.Controls[i]).caption;          l := TRadioButton(rg.Controls[i]).visible;       end;    end;      s will be 'HiddenRadioButton' and l=False when it hits the hidden radiobutton. So I could not use Itemindex as an index. In all my tests the invisible radiobutton was TRadioButton(rg.Controls[1]). But one should not rely on this. So I came to the following code - which even works if itemindes < 0: Code: [Select]var    j,    i: Integer;    lGoOn : boolean; begin    lGoOn := true;    i:= 0;    if (rg.ItemIndex > -1) and (rg.Items.Count > 0)then    begin       j := -1;       while lGoOn AND (i < rg.ControlCount) do       begin          if TRadioButton(rg.Controls[i]).visible then             inc(j);          if j = rg.ItemIndex then          begin             TRadioButton(rg.Controls[i]).SetFocus;             lGoOn := false;          end          else             inc(i);       end;    end    else       if (rg.ItemIndex = -1) and (rg.Items.Count > 0)then       begin          i:= 0;          while lGoOn AND (i < rg.ControlCount) do          begin             if TRadioButton(rg.Controls[i]).visible then             begin                TRadioButton(rg.Controls[i]).SetFocus;                lGoOn := false;             end             else                inc(i);          end;       end; end; ---------------------------------------------------------------------- howardpc · 2014-11-06 10:15 Yes. You'll see (before you had replied) that I edited my original code using Code: [Select] ...  if Controls[i] is TRadioButton then  ... to code using FindComponent(), since this is more specific, and covers cases where your form might have individual radiobuttons in addition to one or more radiogroups. ---------------------------------------------------------------------- hy · 2014-11-06 19:57 No I am only checking the controls that belong to the radiogroup in question. Thanks.