Solved

TStringList

Posted on 2003-11-07
6
1,183 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now