Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Como abrir varios archivos.txt en un mismo Memo (https://www.clubdelphi.com/foros/showthread.php?t=88293)

albelg 15-05-2015 20:19:40

Como abrir varios archivos.txt en un mismo Memo
 
hola a todos los colegas del club, soy nuevo y necesito una colaboracion sobre como abrir varios archivos.txt en un mismo memo sin aplastarlos. o sea cuando abro los txt solo me muestra en el memo el ultimo de ellos. gracias de antemano

ecfisa 15-05-2015 21:49:27

Hola albelg, bienvenido a Club Delphi :)

Como acostumbramos con los que se inician, te invitamos a que leas nuestra guía de estilo.

Revisa este ejemplo:
Código Delphi [-]
...
implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenDialog1.Filter := 'Archivos de texto|*.TXT'
  with Memo1 do
  begin
    // Limpiar
    Lines.Clear;
    // Scroll vertical
    ScrollBars := ssVertical;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ts: TStrings;
begin
  if OpenDialog1.Execute then
  begin
    ts := TStringList.Create;
    try
      // agregar archivo texto
      ts.LoadFromFile(OpenDialog1.FileName);
      Memo1.Lines.AddStrings(ts);
      // ir al final
      Memo1.SelStart  := Length(Memo1.Text);
      Memo1.SelLength := 0;
      Memo1.SetFocus;
    finally
      ts.Free;
    end;
  end;
end;

Saludos :)

nlsgarcia 15-05-2015 22:11:20

albelg,

Cita:

Empezado por albelg
...como abrir varios archivos.txt en un mismo memo...

¡Bienvenido al Club Delphi! :D

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
   openDialog : TOpenDialog;
   FileList : TStringList;

begin

   openDialog := TOpenDialog.Create(self);
   openDialog.InitialDir := GetCurrentDir;
   openDialog.Options := [ofFileMustExist];
   openDialog.Filter := 'Text Files | *.txt';

   if openDialog.Execute then
   begin
      FileList := TStringList.Create;
      FileList.LoadFromFile(openDialog.FileName);
      Memo1.Text := Memo1.Text + FileList.Text;
   end;

   openDialog.Free;
   FileList.Free;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   Memo1.Clear;
end;

end.
El código anterior en Delphi 7 sobre Windows 7 Professional x32, Permite abrir varios archivos .txt dentro de un TMemo uno a continuación del otro, como se muestra en la siguiente imagen:



Espero sea útil :)

Nelson.

ecfisa 16-05-2015 01:31:06

Hola de nuevo.

Esta variante del código del mensaje #2, te permite hacer una o múltiples selecciónes de los archivos de texto que desees concatenar en el memo, en una sola operación:
Código Delphi [-]
procedure TForm1.FormCreate(Sender: TObject);
begin
  with OpenDialog1 do
  begin
    Filter:= 'Archivos de texto|*.TXT';
    // permitir selección múltiple
    Options:= Options + [ofAllowMultiSelect];
  end;
  with Memo1 do
  begin
    // Limpiar
    Lines.Clear;
    // Scroll vertical
    ScrollBars := ssVertical;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ts: TStrings;
  i : Integer;
begin
  if OpenDialog1.Execute then
  begin
    ts := TStringList.Create;
    try
      // agregar archivos de texto
      for i := 0 to OpenDialog1.Files.Count - 1 do
      begin
        ts.Clear;
        ts.LoadFromFile(OpenDialog1.Files[i]);
        Memo1.Lines.AddStrings(ts);
      end;
      // ir al final
      Memo1.SelStart  := Length(Memo1.Text);
      Memo1.SelLength := 0;
      Memo1.SetFocus;
    finally
      ts.Free;
    end;
  end;
end;
Dentro del cuadro de diálogo, la selección múltiple se realiza igual que en windows por ej.: Ctrl+Left Click ~ Shift+KeyDown/KeyUp/PageDown/PageUp ~ etc.

Saludos :)

nlsgarcia 17-05-2015 05:03:07

albelg,

Cita:

Empezado por albelg
...como abrir varios archivos.txt en un mismo memo...

:rolleyes:

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
   Memo1.ScrollBars := ssBoth;
   Memo1.WordWrap := False;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   openDialog : TOpenDialog;
   FileList : TStringList;
   i : Integer;

begin

   openDialog := TOpenDialog.Create(self);
   openDialog.InitialDir := GetCurrentDir;
   openDialog.Options := [ofFileMustExist, ofAllowMultiSelect];
   openDialog.Filter := 'Text Files | *.txt';

   if openDialog.Execute then
   begin
      for i := 0 to OpenDialog.Files.Count - 1 do
      begin
         FileList := TStringList.Create;
         FileList.LoadFromFile(openDialog.Files[i]);
         Memo1.Text := Memo1.Text + FileList.Text;
         FileList.Free;
      end;
   end;

   openDialog.Free;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   Memo1.Clear;
end;

end.
El código anterior (Versión 2 del Msg #3) en Delphi 7 sobre Windows 7 Professional x32, Permite abrir varios archivos .txt dentro de un TMemo uno a continuación del otro, como se muestra en la siguientes imágenes:





Espero sea útil :)

Nelson.

albelg 18-05-2015 17:16:39

gracias a todos me sirvio de mucho. pronto los molestare para algo mas


La franja horaria es GMT +2. Ahora son las 21:51:25.

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