We help IT Professionals succeed at work.

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

Peter Kiers
Peter Kiers asked
on
242 Views
Last Modified: 2012-05-08
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

Comment
Watch Question

Emmanuel PASQUIERFreelance Project Manager
CERTIFIED EXPERT
Top Expert 2010

Commented:
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;
Emmanuel PASQUIERFreelance Project Manager
CERTIFIED EXPERT
Top Expert 2010

Commented:
the last function 'get' should be called something like 'getLine' .
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
you allready this question:
https://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_21849969.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 ?
Oracle dba
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Emmanuel PASQUIERFreelance Project Manager
CERTIFIED EXPERT
Top Expert 2010

Commented:
>> 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
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
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
Peter KiersOperator

Author

Commented:
Thanx, Peter kiers

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.