Solved

TStringList

Posted on 2003-11-07
6
1,224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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