Create Radiogroup based on Combobox selection using INI, dynamically in Delphi

Asked

Viewed 364 times

1

Context:
With the use of a personal app made on , I have been adding new actions as needed in a TRadioGroup, but now contains many items (16), harming the of the application.

What I wanted to do was categorize these items from TRadioGroup in a TComboBox, so it would have some categories and the TRadioGroup would take fewer options, thus improving the visual layout and facilitating choice.

Need:
Create a TComboBox dynamically based on a file .ini, as if if it were a configuration for loading the `Tcombobox, it would look something like this:

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

With the category defined and clicking on TComboBox then load the items from TRadioGroup based on the group, a .ini, sort of like this, 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 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

1 answer

1


To solve the problem, there should be a way to load the TComboBox based on the . ini already created:

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

A for would solve, but for each category should load its items, so it is necessary to have one more for. I changed the .ini of the items 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 dynamically solve this:

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 of the solution?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.