Re: Re: Listbox+files.txt+memo


Posted by mnemo on November 15, 2000 at 15:27:22:

In Reply to: Re: Listbox+files.txt+memo posted by Ulrich Becker on November 15, 2000 at 15:10:55:

The same thing can also be done with:

procedure TForm1.ListBox1Click(Sender: TObject);
var
  Txt: TStringList;
begin
  Txt := TStringList.Create;
  Txt.LoadFromFile('E:\MD\TEXT\'+ListBox1.Items[ListBox1.ItemIndex]);
  Memo1.Lines.Assign(Txt);
  Txt.Free;
end;

However, this code would be quite error prone so I could prefer this code instead:

procedure TForm1.ListBox1Click(Sender: TObject);
var
  Txt: TStringList;
  Filename: string;
begin
  if ListBox1.ItemIndex<>-1 then
  begin
    Txt := TStringList.Create;
    try
      Filename := 'E:\MD\TEXT\'+ListBox1.Items[ListBox1.ItemIndex];
      if FileExists(Filename) then
      begin
        Txt.LoadFromFile(Filename);
        Memo1.Lines.Assign(Txt);
      end else
        Memo1.Clear;
    finally
      Txt.Free;
    end;
  end;
end;

(or even better, one could use a try except statement to catch all execptions and abort the operation)

Good Luck!

/m

: procedure TForm1.lstBoxClick(Sender: TObject);
: var i : Integer;
: s : String;
: tf : Textfile;

: begin
: s := '';
: for i := 0 to pred(lstBox.Items.Count) do
: begin
: if lstBox.Selected[i] then
: s := lstBox.Items[i];
: end;
: if s <> '' then
: begin
: s := 'c:\' + s;
: memo1.Lines.Clear;
: assignFile (tf,s);
: reset (tf);
: while not eof(tf) do
: begin
: readln (tf,s);
: memo1.Lines.Add (s);
: end;
: closeFile (tf);
: end;
: end;




Related Articles and Replies: