Create RadioGroup based on selection of ComboBox using INI, dynamically in Delphi

0

Context:
Using a personal application made in , I was adding new actions as needed on a TRadioGroup , but now it contains many items (16), harming the of the application.

What I wanted to do would be to categorize these items from TRadioGroup into TComboBox , so I would have some categories and TRadioGroup would be less options, thus improving visual layout and making it easier to choose.

Need:
Create a TComboBox dynamically based on a .ini file, as if was a configuration for loading 'TComboBox, it would look something like this:

[CATEGORIA]
0=CATEGORIA1
1=CATEGORIA2
2=CATEGORIA3

With the category defined and loading in TComboBox then load the items from TRadioGroup based on the group, a .ini , more or less so, I thought:

[CATEGORIA_1]
0=ITEM1
1=ITEM2

[CATEGORIA_2]
0=ITEM1
1=ITEM2
2=ITEM3
3=ITEM4
4=ITEM5

[CATEGORIA_3]
0=ITEM1
1=ITEM2
2=ITEM3

Currently my code using the INI is only to save the last selected option, and when reopening the program already bring:

.INI

[CONFIG]
INDEXGROUP=1

Procedure SalvarINI :

procedure SalvarINI;
var
   vlIni: TIniFile;
begin
  try
    vlIni := TIniFile.Create(ExtractFileDir(Application.ExeName) +'\'+ ExtractFileName(Application.ExeName)+'.ini');
    try
      vlIni.WriteInteger('CONFIG', 'INDEXGROUP',rgItens.ItemIndex);

      vlIni.UpdateFile;
    finally
      vlIni.Free;
    end;
  except
    on e: exception do
    begin
      ShowMessage('Ops..não conseguimos Salvar as configurações: '+ e.Message);
    end;
  end;
end;

carregarINI

procedure carregarIni;
var
  vlIni: TIniFile;
begin
  vlIni := TIniFile.Create(ExtractFileDir(Application.ExeName) +'\'+ ExtractFileName(Application.ExeName)+'.ini');

  try
    RGProduto.ItemIndex := vlIni.ReadInteger('CONFIG' ,'INDEXGROUP',1);
  finally
    FreeAndNil(vlIni);
  end;
end;

Thank you

    
asked by anonymous 13.05.2016 / 16:05

1 answer

0

To solve the problem, you should have a way to load TComboBox so based on the .ini already created:

[CATEGORIA]
0=CATEGORIA1
1=CATEGORIA2
2=CATEGORIA3

A for would solve, but for each category you should load your items, so you need a for . I've changed the%% of items by a little to make the algorithm easier:

[CATEGORIA_1]
0ITEM=DESCRICAOITEM0CAT1
1ITEM=DESCRICAOITEM1CAT1

[CATEGORIA_2]
0ITEM=DESCRICAOITEM0CAT2
1ITEM=DESCRICAOITEM1CAT2
2ITEM=DESCRICAOITEM1CAT2

[CATEGORIA_3]
0ITEM=DESCRICAOITEM0CAT3
1ITEM=DESCRICAOITEM1CAT3
2ITEM=DESCRICAOITEM2CAT3

So I created a procedure to resolve this dynamically:

procedure TFormF.carregaRadioGroup;
var
  i,j : Integer;
  strValGrupo,
  strProdutos : TStringList;
  vlCopy : String;
  vlGrupoProduto : String;
  count : integer;
  LIni: TIniFile;
begin

  strValGrupo := TStringList.Create;
  strProdutos := TStringList.Create;

  try
    rgDinamicoProduto.Items.Clear;
    LIni := TIniFile.Create(ExtractFileDir(Application.ExeName) +'\'+ ExtractFileName(Application.ExeName)+'.ini');

    if LIni.SectionExists('CATEGORIA') then
    begin
      LIni.ReadSection('CATEGORIA',strValGrupo);

      for i:=1 to strValGrupo.Count do
      begin
        vlGrupoProduto := 'CATEGORIA_'+IntToStr(i-1);

        if LIni.SectionExists(vlGrupoProduto) then
        begin
          LIni.ReadSection(vlGrupoProduto, strProdutos);

          if (cbGrupo.ItemIndex = i-1) then
          begin
            count := trunc(strProdutos.Count/3);
            for j:=1 to (count) do
            begin
              rgDinamicoProduto.Items.Add(LIni.Readstring(vlGrupoProduto, IntToStr(J-1)+'ITEM',''));
            end;
          end;
        end;
      end;
    end;
  finally
    FreeAndNil(LIni);
  end;
end;

The problem has been solved, but what do you think about the solution?

    
27.06.2016 / 13:36