Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Problema leyendo líneas de texto con variables (https://www.clubdelphi.com/foros/showthread.php?t=86001)

UsuarioBaja123 04-06-2014 18:20:33

Problema leyendo líneas de texto con variables
 
Tengo este código:
Código Delphi [-]
procedure TForm2.Button1Click(Sender: TObject);
var
F: TextFile;
sLinea: String;
button : TButton;
begin
AssignFile( F, ExtractFilePath( Application.ExeName ) + 'program.txt' );
  Reset( F );

  while not Eof( F ) do
  begin
    ReadLn( F, sLinea );
    if sLinea='ButtonCtrl' then
        button := TButton.Create(self);
        ReadLn( F, sLinea);
        if sLinea='1' then
            button.Left:=sLinea;
            ReadLn( F, sLinea);
            if sLinea=[1..400] then
                button.Top:=sLinea;
  end;
  CloseFile( F );
end;

end.

Que me lee un archivo que es así:

Código:

ButtonCtrl
40
25
ButtonCtrl
90
47

Ahora, me gustaría que el programa lea el archivo de texto y cada vez que vea ButtonCtrl cree un botón con los parámetros que siguen que son dos integer (X e Y) y lo ponga en el Form. Como si yo pongo 30 líneas con ButtonCtrl con dichas variables me los ponga (infinito) y que si solo pongo ButtonCtrl y no pongo las dos variables a continuación no lo cree o de un mensaje de error.

Parece fácil pero me lié y no lo he continuado.

Si además pueden, se podría hacer que el programa reconociera el archivo de texto así:

Código:

ButtonCtrl{
POS=40,20
CAP=Ejemplo
}
ButtonCtrl{
POS=67,90
CAP=Unbotóncualquiera
}

Por lo menos decir que hago mal. Muchas gracias y no, no es proyecto de clase, es un proyecto que llevo intentando hacer desde hace tiempo. Y no quiero usar Archivos INIs aunque faciliten mucho esta tarea.

:)

ecfisa 04-06-2014 23:41:26

Hola ZaneMs.

Creo que te sería mas simple usar TStringsList para manipular el archivo de texto, por ejemplo:
Código Delphi [-]
procedure TForm1.CrearBotones(aFileName: TFileName);
var
  c,b: Integer;
begin
  with TStringList.Create do
  try
    LoadFromFile(aFileName);
    c:= 0;
    b:= 1;
    while (c < Count) do
    begin
      if Strings[c] = 'ButtonCtrl' then
        with TButton.Create(Self) do
        begin
          Parent:= Self;
          Name:= 'MiBoton'+IntToStr(b);
          Left:= StrToIntDef(Strings[c+1],0);
          Top := StrToIntDef(Strings[c+2],0);
          Inc(c);
          Inc(b);
        end;
      Inc(c);
    end;
  finally
    Free;
  end;
end;
Pero de todos modos no te recomiendo trabajarlo de ese modo.


Delphi dispone de las clases TIniFile y TRegistry que hacen esa tarea mas simple y segura. Un ejemplo usando IniFiles:
Código Delphi [-]
...
uses IniFiles;

const
  INIFIL = 'C:\CARPETA\ARCHIVO.INI'; // Nombre de archivo

// Este procedimiento crea valores de los atributos  
// de los botones (cinco para el ejemplo)
procedure CrearIniFile;
var
  Ini: TIniFile;
  i: Integer;
  s: string;
begin
  with TIniFile.Create(INIFIL) do
  try
    for i:= 1 to 5 do
    begin
      s:= 'MiBoton'+IntToStr(i);
      WriteString(s, 'Name', s);
      WriteInteger(s,'Left',100);
      WriteInteger(s,'Top', 50*i+25);
      WriteInteger(s,'Height',25);
      WriteInteger(s,'Width',90);
      WriteString(s,'Caption', s);
    end;
  finally
    Free;
  end;
end;

// Se crean los botones y se asignan a sus atributos 
// los valores almacenados en ARCHIVO.INI
procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  s: string;
begin
   if not FileExists(INIFIL) then // Si no fueron definidos los valores,
     CrearIniFile;  //crearlos
  // Crear botones y obtener los valores de sus atributos
  with TIniFile.Create(INIFIL) do
  try
    for i:= 1 to 5 do
    begin
      with TButton.Create(Self) do
      begin
        Parent:= Self;
        s:= 'MiBoton'+IntToStr(i);
        Name:= ReadString(s,'Name','');
        Left:= ReadInteger(s,'Left',0);
        Top:= ReadInteger(s,'Top',0);
        Height:= ReadInteger(s,'Height',25);
        Width:= ReadInteger(s,'Width',70);
        Caption:= ReadString(s,'Caption','Botón');
      end;
    end;
  finally
    Free;
  end;
end;
...
Como te mencioné mas arriba lo mismo podes hacer usando TRegistry, en este enlace: referencia de tedit en TComponentList tenes un ejemplo con ambos modos.

Saludos :)


La franja horaria es GMT +2. Ahora son las 16:43:58.

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