I have recently made a program that stores peoples names, address, etc in a text file. On one of the forms, I want there to be a search option, so the user can search by name and then the persons address is displayed. Like a search engine in a database. I have spent hours trying to work out what the code is for the search button, but its killing me, I can't work it out.

Could someone please help me out with the code.

Regards Satrix

why u dont read every name from the text and see if is the name that u are looking for? how is the data stored in the file? i dont know exacly how delphi works but pascal cant read a word... reads all the line if u are reading a string. so u have to take the string separete it in words.

For a start, delphi, assigns the record to the file, then opens the file for reading to. Then it resets it, then writes to the file and then closes it.

What I'm really asking help for is:

The file has three names in it and each name has an address assigned to it.

e.g.

Scott 32 Roy Close
Katie 31 Roy Close
Chris 2 Roy Close

I type in a name like katie. It then searches the file for the name and then displaies the address that is assosiated to that name.

Is that clearer?

procedure TForm1.Button1Click(Sender: TObject);
var line,name,adress:string;
___f:textfile;
begin
_____assignfile(f,'filename');reset(f);
_____adress:='';
_____repeat
_________readln(f,line);
_________name:=copy(line,1,pos(' ',line)-1);
_________adress:=copy(line,pos(' ',line)+1,length(line));
_____until (lowercase(name)=lowercase(edit1.Text)) or (eof(f));
_____if adress='' then adress:='no name found';
_____edit2.Text:=adress;
_____closefile(f);
end;

my procedure reads the name from edit1 and returns the adress to edit2. replace 'filename' with the name of the file u have

The code that you gave me works in the sence of it displays the address but, only displays the letters in the address not the number, and also you can type in any name and it will display the address. E.g.

The names in the list:

Katie
Andy
Elaine
Scott

You could type in Chris and it will still display one of the address. What ever name you type in it will display the last address that you have entered into the file.

I have tryed to change it but have got stuck. Could you please help me again.

If it helps hears the code for the form:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
details=record
name:string[30];
address:string[50];
end;
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
Edit4: TEdit;
Button2: TButton;
Button3: TButton;
procedure Edit1Change(Sender: TObject);
procedure Edit2Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
detailsrecord:details;
detailsfile:file of details;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1Change(Sender: TObject);
begin
form1.detailsrecord.name:=edit1.text;
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
form1.detailsrecord.address:=edit2.text;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
with form1 do
begin
assignfile(detailsfile,'details.txt');
reset(detailsfile);
seek(detailsfile,filesize(detailsfile));
write(detailsfile,detailsrecord);
closefile(detailsfile);
end;
edit1.Text:='';
edit2.Text:='';
end;

procedure TForm1.Button2Click(Sender: TObject);
var line,name,address:string;
f:textfile;
begin
assignfile(f,'details.txt');reset(f);
address:='';
repeat
readln(f,line);
name:=copy(line,1,pos(' ',line)-1);
address:=copy(line,pos(' ',line)+1,length(line));
until (lowercase(name)=lowercase(edit3.Text)) or (eof(f));
if address='' then address:='no name found';
edit4.Text:=address;
closefile(f);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
edit3.Text:='';
edit4.Text:='';
end;

end.

procedure TForm1.Button1Click(Sender: TObject);
var f:textfile;
name,adress:string;
c:char;
begin
assignfile(f,'c:\details.txt');reset(f);
while not eof(f) do
begin
name:='';
read(f,c);
while c<>' ' do
begin
name:=name+c;
read(f,c);
end;
if lowercase(name)=lowercase(edit1.Text) then
begin
readln(f,adress);
edit2.Text:=adress;
exit;
end else readln(f);
end;
edit2.Text:='name not found';
closefile(f);
end;


this one i actulay tested
it works good:writes corect adress and display 'name not found' if the name is not in the file. and its the best optimize i could get:)... or maybe i shouldnt read all the name if the first letters don't match :idea:

I have a radiogroup with several items in it:

Song 1
Song 2
Song 3
Song 4
etc......

How do I give each item a different command e.g.

song 1: form4.visible:=true;
song 2: form5.visible:=true;

Could someone please help me.

What you need is radiogroupname.itemindex, the item index starts from 0 to refer to a particular radio component. So you can use an if statement on a button click or whatever. This will give you

if radiogroupname.itemindex=0 then
begin
//do song 1 stuff//
end
else if radiogroupname.itemindex=1 then...

and so on.

Hope this helps

This article has been dead for over six months. Start a new discussion instead.