How to save Tcombobox status in a form and when opening it again that is the Tcombobox as before?

0

I have a form with a series of Tcombobox and edits that I would like to accept and save this data and when I open that form it opens the same way I close it, with all the selected Tcombobox etc ...

Work in delphi with pier rad studio xe6.

I already test with the Tformplacement of the rx but either I do something wrong or it does not work ...

    
asked by Roman345 02.11.2016 в 09:42
source

1 answer

2

There are several ways and / or places to store this type of data:

  • Database : For this you must use database access components.
  • Files on disk (INI) : For this Delphi has classes to work with INI files ( TIniFile ).
  • Windows Registry : For this you can use the class that also comes with Delphi ( TRegistry ).
  • ...

With the components that you comment on RxLib , you can do it in the registry or in INI files, since they implement both options.

If you want to try to do it by hand, for example using an INI file, you can use a couple of procedures like these.

In the uses of your unit add:

uses
  Inifiles;

In the public part of your form, add 2 procedures:

  procedure GuardarEstado();
  procedure RecuperarEstado();

The implementation will be somewhat similar to this:

procedure Tfrm1.RecuperarEstado();
var
  iniF:TIniFile;
  fName:String;
begin
  // El fichero se llamará igual que la aplicación pero extensión INI
  fName := ChangeFileExt(Application.ExeName, '.INI');
  // Tenemos que comprobar si el fichero existe...
  if FileExists(FName) then begin
    // Crear el fichero
    iniF := TIniFile.Create(fName);
    // Proteccion para liberar
    try
      // Recuperar los valores....
      edtDescripcion.Text := iniF.ReadString('CONFIG', 'Nombre', '');
      edtCodigo.Text := iniF.ReadString('CONFIG', 'Codigo', '');

      ...     <= El resto de elementos a recuparar

    finally
      // Liberarlo
      FreeAndNil(iniF);
    end;
  end;

end;

procedure Tfrm1.GuardarEstado();
var
  iniF:TIniFile;
  fName:String;
begin
  // El fichero se llamará igual que la aplicación pero extensión INI
  fName := ChangeFileExt(Application.ExeName, '.INI');
  // Crear el fichero
  iniF := TIniFile.Create(fName);
  // Proteccion para liberar
  try
    // Guardar los valores...
    iniF.WriteString('CONFIG', 'Nombre', edtDescripcion.Text);
    iniF.WriteInteger('CONFIG', 'Codigo', StrToInt(edtCodigo.Text));

    ...     <= Los elementos que quieras guardar

  finally
    // Liberarlo
    FreeAndNil(iniF);
  end;
end;

You should be adding more components in each procedure. As many as you want to store.

Finally, for example, in the OnShow of your form, you should call the procedure RecuperarEstado , and in the OnClose of the forms at GuardarEstado .

In my case, once the state is saved, in the same place where the EXE is generated, an .INI file appears with a content like this:

  

[CONFIG]

     

Code = 1

     

Name = SEUR

As you add more components and properties, they will be stored in this file.

    
answered by 02.11.2016 / 11:42
source