[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

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

0
peterkiers
Asked:
peterkiers
  • 4
  • 2
2 Solutions
 
peterkiersAuthor Commented:
Increase 250
0
 
mikelittlewoodCommented:
First of all you could have just assigned the strings or equated them to the memo

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

Im not sure how you would do the second solution, but to do the first solution wouldnt be too hard as long as you keep the file stored in the stringlist rather than freeing it.

Code in the OnKeyDown event of the main form and set KeyPreview property of main form to true.
Keep an integer value globally so that you can store the current position (iCurStr)

// first check to see if there is anything in the stringlist
if sl.count = 0 then exit;

if key = VK_DOWN then  // think this is the value but not at PC to check
begin
  // check if at last position
  if iCurStr = sl.count then exit;
  // if not at last position then put in status bar and increment count
  // x is whatever panel you are writing to
  inc(iCurrStr);
  StatusBar1.Panels[x] = sl.strings[iCurrStr-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(iCurrStr);
  StatusBar1.Panels[x] = sl.strings[iCurrStr-1];
end;
0
 
peterkiersAuthor Commented:
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
2266180Commented:
for the second option:
if you prefer to have speed and memory is not an issue, then read the file in a stringlist and do just like mike did.
if you realy want to read it from disk then something like this shoould do it:

var line:integer=0;// global (or declared in the form

....

function getNextLine(filename:string):string;
var f:textfile; s:string; i:integer;
begin
  i:=0;
  s:='';
  assignfile(f,filename);
  try
    reset(f);
    try
      while (i<=line) and (not eof(f)) do
        readln(f,s);
      if i<=line then s:='';// eof reached
    finally
      closefile(f);
    end;
  except
  end;
  result:=s;
end;

and use this on the keypress event increasing and decreasing line as needed.

about the access violation: which line is it?
0
 
peterkiersAuthor Commented:
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;
0
 
2266180Commented:
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 ;)
0
 
peterkiersAuthor Commented:
Thanx. Problem solved.

Peter Kiers
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now