Solved

TStringList

Posted on 2003-11-07
6
1,216 Views
Last Modified: 2008-02-01
I am trying to read two lines from a text file into separate Edit fields  on a form with one button, so that when i click the button the lines are loaded into the fields.  So for example the contents of the text files are  

Green
Black

When i press the button i would like "green" to be loaded to the first Edit box and Black to be loaded into the second Edit Box. I have written the following code which i could not get to work, i get many compiler errors so i am guessing it is not very correct

implementation

{$R *.xfm}

procedure ReadFromFile(Edit1 :TEdit);

TForm1.ButtonLoadClick(Sender :TObject);

var
sl: TStringList;
begin
sl :=TStringList.Create;
sl.LoadFromFile('/home/user/test.txt');
Edit1.Text := sl[0]
Edit2.Text := sl[1]
sl.Destroy;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ReadFromFile(Edit1.Text);
ReadFromFile(Edit2.Text);
end;

end.
0
Comment
Question by:itektas
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 9702860
OK.

Get rid of the line TForm1.ButtonLoadClick( Sender : TObject )

in the 'uses' bit

where it says something like

TMyForm = class(TForm)
private:
 

add :

procedure ReadFromFile;

then change the implementation bit to:


procedure ReadFromFile;
var
sl: TStringList;
begin
sl :=TStringList.Create;
sl.LoadFromFile('/home/user/test.txt');
Edit1.Text := sl[0]
Edit2.Text := sl[1]
sl.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ReadFromFile;
end;

I think.
0
 

Author Comment

by:itektas
ID: 9703041
ok i have changed to the following but it doesnt seem to work yet, when compiling it says there are many errors, such as

[Error] Unit1.pas(34): Undeclared identifier: 'Edit1'
[Error] Unit1.pas(34): '(' expected but ':=' found
[Error] Unit1.pas(35): Missing operator or semicolon
[Error] Unit1.pas(35): '(' expected but ':=' found
[Error] Unit1.pas(36): Missing operator or semicolon
[Error] Unit1.pas(37): '.' expected but ';' found
[Error] Unit1.pas(14): Unsatisfied forward or external declaration: 'TForm1.ReadFromFile'

  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    procedure ReadFromFile;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.xfm}

var
sl: TStringList;
begin
sl :=TStringList.Create;
sl.LoadFromFile('/home/user/test.txt');
Edit1.Text := sl[0]
Edit2.Text := sl[1]
sl.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ReadFromFile;
end;
end.


0
 

Author Comment

by:itektas
ID: 9703052
I forgot to include it in my previous comment but i did actually include "procedure ReadFromFile;" in the beginning of the implementation; thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:andrewjb
ID: 9703084
Needs to be

procedure TForm1.ReadFromFile;

within the implementation section

The function is part of the 'TForm1' class
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 20 total points
ID: 9703089
and you need a ; after s[0] and s[1]
0
 

Author Comment

by:itektas
ID: 9703137
Excellent! Compiles fine now , thanks a lot
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question