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

asked on

Display each line from a file on a statusbar by keypress iniated by a toolbar button.

Hi,

Can someone tell me how to display each line of a certain file on a statusbar
by each keypress initiated by a toolbar button. I have found this function,
but don't know how to implement it. Can someone help me?

PK
var
  Form1: TForm1;
  line:integer=0;
 
implementation
 
{$R *.dfm}
 
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;

Open in new window

Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

That's not a good idea to do it by re-reading each line until you find the one needed. besides, there is missing something on this algo to work :
do
  readln(f,s);
  inc(i);
end;
and line should not be a global variable but a parameter.

I would strongly recommend the use of TStringList :
- you create your list object once only (by the main form)
- you destroy it at the detruction of the form
- at some point, you read your while file into it
- and you access it line by line at will.

TForm1= class(TForm)
...
private
  _MyStrList:TStringList;
...
end;

procedure TForm1.FormCreate(Sender:TObject);
begin
  _MyStrList:=TStringList.Create;
...
end;

procedure TForm1.FormDestroy(Sender:TObject);
begin
  _MyStrList.Free;
...
end;

procedure TForm1.LoadFile(filename:string);
begin
 _MyStrList.LoadFromFile(filename);
// maybe something like _Line:=0; to reset position when loading a new file
end;

then, instead of calling your getNextLine, you simply use _MyStrList[_Line] to get the line n°_Line
You can test for _MyStrList.Count before, to avoid error

function TForm1.get(L:integer):string;
begin
 if (L>=0) And (L<_MyStrList.Count)
   Then Result:=_MyStrList[L]
   Else Result:='';
end;
the last function 'get' should be called something like 'getLine' .
you allready this question:
https://www.experts-exchange.com/questions/21849969/A-tstringlist-question.html

and you have pulled "line: integer = 0;" out of it's context

personally i don't see any use whatsoever to read a text line using the statusbar
so why do you wanna do that, what's the point ?
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium 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
and don't come around saying you don't want to use TActionList
this is the easiest way to use shortcut keys
without have to hassle around yourself with keypreview

use F5-F6 alternatively to go up or down the file after loading

--dfm--
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'frmFileOnStatusbar'
  ClientHeight = 426
  ClientWidth = 937
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  KeyPreview = True
  OldCreateOrder = False
  OnKeyDown = FormKeyDown
  PixelsPerInch = 96
  TextHeight = 13
  object sbFile: TStatusBar
    Left = 0
    Top = 407
    Width = 937
    Height = 19
    Panels = <
      item
        Text = 'Line'
        Width = 70
      end
      item
        Width = 50
      end>
  end
  object btnLoadFile: TButton
    Left = 48
    Top = 39
    Width = 75
    Height = 25
    Caption = 'Load File'
    TabOrder = 1
    OnClick = btnLoadFileClick
  end
  object Memo1: TMemo
    Left = 480
    Top = 8
    Width = 257
    Height = 361
    Lines.Strings = (
      'Memo1')
    TabOrder = 2
  end
  object odFile: TOpenDialog
    Filter = 'All files|*.*'
    Title = 'Open file'
    Left = 256
    Top = 24
  end
  object alKeys: TActionList
    Left = 376
    Top = 8
    object actKeyUp: TAction
      Caption = 'Key Up'
      ShortCut = 116
      OnExecute = actKeyUpExecute
    end
    object actKeyDown: TAction
      Caption = 'KeyDown'
      ShortCut = 117
      OnExecute = actKeyDownExecute
    end
  end
end

Open in new window

>> you like underscores a lot don't you ?
yes, I use it where almost everybody else use f to state it is a private variable. I find it easier to read and never quite understood why f stands for... so if you can explain it to me, I might not change my habits but out of curiosity I would be glad
f stands for field
i'd need to search where that's stated

some delphi code in the beginning had m for method too,
but that was dropped soon
Avatar of Peter Kiers

ASKER

Thanx, Peter kiers