Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Memorizar opciones con CheckBox (https://www.clubdelphi.com/foros/showthread.php?t=92767)

gdlrinfo 25-01-2018 17:50:48

Memorizar opciones con CheckBox
 
Estimados quisiera saber como hacer para mantener una elección de un CheckBox en una aplicación.-
Por ejemplo .

Quiero dejar 2 checkbox tildados y que cuando alguien cierre la aplicación sigan marcados o desmarcados (depende lo que el usuario ponga), es decir que se memorice si fue marcado o no !

Desde ya muchas gracias .--

duilioisola 25-01-2018 18:13:52

Una opción rápida es generar un archivo INI con esos datos.
Al abrir la aplicación lo lees.
Al cerrar lo guardas.

Código Delphi [-]
function LeeDatoIni(FicheroINI: string; const Section, Ident: string; Default: string = ''): string; overload;
begin
  with TIniFile.Create(FicheroINI) do
  begin
     try
        Result := ReadString(Section, Ident, Default);
     finally
        Free;
     end;
  end;
end;

function LeeDatoIni(FicheroINI: string; const Section, Ident: string; Default: integer = 0): integer; overload;
begin
  with TIniFile.Create(FicheroINI) do
  begin
     try
        Result := ReadInteger(Section, Ident, Default);
     finally
        Free;
     end;
  end;
end;

function LeeDatoIni(FicheroINI: string; const Section, Ident: string; Default: TDateTime = 0): TDateTime; overload;
begin
  if (Default = 0) then
     Default := Now;
  with TIniFile.Create(FicheroINI) do
  begin
     try
        Result := ReadDateTime(Section, Ident, Default);
     finally
        Free;
     end;
  end;
end;

procedure EscribeDatoIni(FicheroINI: string; const Section, Ident, Dato: string); overload;
begin
  with TIniFile.Create(FicheroINI) do
  begin
     try
        WriteString(Section, Ident, Dato);
     finally
        Free;
     end;
  end;
end;

procedure EscribeDatoIni(FicheroINI: string; const Section, Ident: string; Dato: integer); overload;
begin
  with TIniFile.Create(FicheroINI) do
  begin
     try
        WriteInteger(Section, Ident, Dato);
     finally
        Free;
     end;
  end;
end;

procedure EscribeDatoIni(FicheroINI: string; const Section, Ident: string; Dato: TDateTime); overload;
begin
  with TIniFile.Create(FicheroINI) do
  begin
     try
        WriteDateTime(Section, Ident, Dato);
     finally
        Free;
     end;
  end;
end;


var
  FicheroINI : string;
begin
   // El ini es el nombre de la aplicacio + .INI
   FicheroINI := ChangeFileExt(ExtractFileName(Application.ExeName), '.INI');

   // Leo el estado de los checkbox
   Checkox1.Checked := (LeeDatoIni(FicheroINI, 'EstadoCheckBox', 'CheckBox1', 0) = 1);
   Checkox2.Checked := (LeeDatoIni(FicheroINI, 'EstadoCheckBox', 'CheckBox2', 0) = 1);
   ...

   // Guardo el estado de los checkbox
   if Checkox1.Checked then
      EscribeDatoIni(FicheroINI, 'EstadoCheckBox', 'CheckBox1', 1)
   else
      EscribeDatoIni(FicheroINI, 'EstadoCheckBox', 'CheckBox1', 0);

   if Checkox2.Checked then
      EscribeDatoIni(FicheroINI, 'EstadoCheckBox', 'CheckBox2', 1)
   else
      EscribeDatoIni(FicheroINI, 'EstadoCheckBox', 'CheckBox2', 0);

end;

gdlrinfo 25-01-2018 18:26:28

GRaciaS
 
Muchas Gracias Voy a probarlo !

Neftali [Germán.Estévez] 26-01-2018 09:30:15

Si tu aplicación trabaja con Bases de Datos, otra opción es guardar esa configuración en una tabla de la Base de Datos (una tabla de CONFIGURACION).


La franja horaria es GMT +2. Ahora son las 10:10:00.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi