Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

A tstringlist - question

Hi there,

I want to load the text of a file in a stringlist, and that i allready did:

procedure TForm2.Button4Click(Sender: TObject);
var
  sl: TStringList;
begin
  sl := TStringList.Create;
  try
    sl.LoadFromFile(edit1.text);
    mainform.Memo1.SetSelTextBuf(PChar(sl.Text));
  finally
    sl.Free;
  end;
end;  

But now I have 2 questions:

Is this possible to make a keydown-event and every time I press the arrow-
down button I get to see the first string in the statusbar. And when I press
again I get the next string etc.. etc... until end of string.

or

Is it perhaps possible to skip the stringlist, and just everytime I press the
arrow-down button the programm reads the first line of the textfile
and when I press again the second line until eof.

If its possible i prefer the second option.

Peter Kiers


Peter Kiers

Avatar of Peter Kiers
Peter Kiers
Flag of Netherlands image

ASKER

Increase 250
SOLUTION
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I dont know if i did something wrong but I made this and i got an access violation:

var
  MainForm: TMainForm;
  iCurStr: integer;
  sl: TStringList;


procedure TMainForm.Button3Click(Sender: TObject);
begin
  sl := TStringList.Create;
  try
    sl.LoadFromFile(edit1.text);
    Memo1.text := sl.text;
  finally
    sl.Free;
  end;
end;


procedure TMainForm.Button4Click(Sender: TObject);
begin
  if Opendialog1.Execute then
  Edit1.Text:=Opendialog1.FileName;
end;

procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if iCurStr = sl.count then exit;
  inc(iCurStr);
  StatusBar1.Panels[0].text := (sl.strings[iCurStr-1]);
end;

procedure TMainForm.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if iCurStr = 1 then exit;
  dec(iCurStr);
  StatusBar1.Panels[0].text := (sl.strings[iCurStr-1]);
end;

peter
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if sl.count = 0 then exit;                     //at this line i get an access violation

if key = VK_DOWN then
begin
  if iCurStr = sl.count then exit;
  inc(iCurStr);
  StatusBar1.Panels[0].text := (sl.strings[iCurStr-1]);
end
else
if key = VK_UP then
begin
  // check if at first position
  if iCurStr = 1 then exit;
  // if not at first position then put in status bar and deccrement count
  dec(iCurStr);
  StatusBar1.Panels[0].text := (sl.strings[iCurStr-1]);
end;
end;
that is becaus in button3 click, you not just create sl, but also free it, so sl is no longer initialized ;)
better create sl and formcreate and destroy it at formdestroy ;)
Thanx. Problem solved.

Peter Kiers