Solved

TStringList

Posted on 2003-11-07
6
1,199 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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