2006. július 17., hétfő

Write the checked state of stand-alone TRadioButtons to a TIniFile


Problem/Question/Abstract:

I have three single TRadioButtons sitting on a TPanel (on Form1). How can I save the state of the checked TRadioButton to a TIniFile and read it back afterwards? I know how to do it with a TRadioGroup, but not with stand-alone radiobuttons.

Answer:

uses
  IniFiles;

procedure SaveRadioButtonState;
var
  data: TIniFile;
  K: Integer;
begin
  data := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'test.ini');
  try
    for K := 0 to form1.ComponentCount - 1 do
      if Form1.Components[K] is TRadioButton then
        {We only look for the checked radiobutton and save its state, of course}
        data.WriteBool('Options', IntToStr(K),
          TRadioButton(Form1.Components[K]).Checked = True);
  finally
    data.Free;
  end;
end;

procedure ReadRadioButtonState;
var
  data: TIniFile;
  K: Integer;
begin
  data := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'test.ini');
  try
    for K := 0 to Form1.ComponentCount - 1 do
      if Form1.Components[K] is TRadioButton then
        TRadioButton(form1.Components[K]).Checked := data.ReadBool('Options',
          IntToStr(K), True);
  finally
    data.Free;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése