i am trying to create a component where when the button is clicked:
text is read from an edit box,
then a listbox is searched for the entry
if/when found the file should load into a memobox
so far i can get the subcomponents onto the form, i enter the items in the listbox via the object inspector
when i run the component on a form the message 'file not found' (my message)appears
any suggestions are much appreiciated
regards
my code is below - i think i need to link the function 'find' somehow
unit SearchAndLoad;
interface
uses
SysUtils, Classes, Controls, ExtCtrls, stdctrls, dialogs;
type
TSearchAndLoad = class(TCustomPanel)
FLoadButton: TButton;
fsearchedit: Tlabelededit;
fFileDisplay: Tmemo;
FFileList: TListbox;
private
FOnClick_FLoadButton: TNotifyEvent;
procedure myClick( sender : TObject );
{ Private declarations }
protected
function find( ffilelist : TListBox; FileToFind : string ) : integer; virtual;
property labelededit1: Tlabelededit read fsearchedit write fsearchedit;
property memo1: Tmemo read fFileDisplay write fFileDisplay;
property Buttton1: TButton read FLoadButton write FLoadButton;
{ Protected declarations }
public
constructor create(AOwner:tcomponent); override;
destructor destroy; override;
{ Public declarations }
published
property onclick_floadbutton:tNotifyEvent read fOnClick_FLoadButton write fOnClick_fLoadButton;
property Listbox11: Tlistbox read FFileList write FFileList;
{ Published declarations }
end;
var
index:integer;
fileToFind : string;
const
fileExtension = '.txt';
procedure Register;
implementation
function TSearchAndLoad.find( fFilelist : TListBox; fileToFind : string ) : Integer;
var
found : boolean;
index, position : integer;
begin
FileToFind:=fsearchedit.text;
position := -1;
index := 0;
found := false;
while not found and ( index < ffilelist.count ) do
begin
if ffilelist.Items[ index ] <> FileToFind then
inc( index )
else
begin
found := true;
position := index;
end;
end;
result := position;
end;
procedure TSearchAndLoad.myClick( sender : TObject );
begin
inherited click;
fileToFind := fsearchedit.Text + fileExtension;
index:=find( fFilelist, filetofind );
begin
if index = -1 then showmessage ('Sorry date selected can not be found.')
else
begin if fileExists(fFilelist.items[index]) then
fFiledisplay.lines.loadfromfile(fFilelist.items[index])
else
showmessage('Sorry unable to load the specified file.');
end;
end;
if assigned(fOnClick_FLoadButton) then
fOnClick_FLoadButton(self);
end;
constructor TSearchAndLoad.create(Aowner:Tcomponent);
begin
inherited create(AOwner);
Name:='';
height:=225;
width:=355;
FSearchEdit := TlabeledEdit.create(self);
FFileDisplay := Tmemo.create(self);
FFileList := TListbox.create(self);
FLoadButton := TButton.create(self);
with fsearchedit do
begin
top:=20;
left:=10;
caption:='';
labelededit1.editlabel.caption:='Please Enter Filename and Click Button to Load';
parent:=self;
end;
with FFileDisplay do
begin
top:=60;
left:=10;
width:=200;
height:=150;
parent:=self;
end;
with FFileList do
begin
top:=60;
left:=220;
//ffilelist.items:=('Bob');
parent:=self;
end;
with fLoadButton do
begin
top:=20;
left:=220;
caption:='Load';
parent:=self;
onclick:= myClick;
end;
end;
destructor TSearchAndLoad.destroy;
begin
fsearchEdit.free;
FFileDisplay.free;
FFileList.Free;
FLoadButton.Free;
inherited destroy;
end;
procedure Register;
begin
RegisterComponents('Samples', [TSearchAndLoad]);
end;
end.
text is read from an edit box,
then a listbox is searched for the entry
if/when found the file should load into a memobox
so far i can get the subcomponents onto the form, i enter the items in the listbox via the object inspector
when i run the component on a form the message 'file not found' (my message)appears
any suggestions are much appreiciated
regards
my code is below - i think i need to link the function 'find' somehow
unit SearchAndLoad;
interface
uses
SysUtils, Classes, Controls, ExtCtrls, stdctrls, dialogs;
type
TSearchAndLoad = class(TCustomPanel)
FLoadButton: TButton;
fsearchedit: Tlabelededit;
fFileDisplay: Tmemo;
FFileList: TListbox;
private
FOnClick_FLoadButton: TNotifyEvent;
procedure myClick( sender : TObject );
{ Private declarations }
protected
function find( ffilelist : TListBox; FileToFind : string ) : integer; virtual;
property labelededit1: Tlabelededit read fsearchedit write fsearchedit;
property memo1: Tmemo read fFileDisplay write fFileDisplay;
property Buttton1: TButton read FLoadButton write FLoadButton;
{ Protected declarations }
public
constructor create(AOwner:tcomponent); override;
destructor destroy; override;
{ Public declarations }
published
property onclick_floadbutton:tNotifyEvent read fOnClick_FLoadButton write fOnClick_fLoadButton;
property Listbox11: Tlistbox read FFileList write FFileList;
{ Published declarations }
end;
var
index:integer;
fileToFind : string;
const
fileExtension = '.txt';
procedure Register;
implementation
function TSearchAndLoad.find( fFilelist : TListBox; fileToFind : string ) : Integer;
var
found : boolean;
index, position : integer;
begin
FileToFind:=fsearchedit.text;
position := -1;
index := 0;
found := false;
while not found and ( index < ffilelist.count ) do
begin
if ffilelist.Items[ index ] <> FileToFind then
inc( index )
else
begin
found := true;
position := index;
end;
end;
result := position;
end;
procedure TSearchAndLoad.myClick( sender : TObject );
begin
inherited click;
fileToFind := fsearchedit.Text + fileExtension;
index:=find( fFilelist, filetofind );
begin
if index = -1 then showmessage ('Sorry date selected can not be found.')
else
begin if fileExists(fFilelist.items[index]) then
fFiledisplay.lines.loadfromfile(fFilelist.items[index])
else
showmessage('Sorry unable to load the specified file.');
end;
end;
if assigned(fOnClick_FLoadButton) then
fOnClick_FLoadButton(self);
end;
constructor TSearchAndLoad.create(Aowner:Tcomponent);
begin
inherited create(AOwner);
Name:='';
height:=225;
width:=355;
FSearchEdit := TlabeledEdit.create(self);
FFileDisplay := Tmemo.create(self);
FFileList := TListbox.create(self);
FLoadButton := TButton.create(self);
with fsearchedit do
begin
top:=20;
left:=10;
caption:='';
labelededit1.editlabel.caption:='Please Enter Filename and Click Button to Load';
parent:=self;
end;
with FFileDisplay do
begin
top:=60;
left:=10;
width:=200;
height:=150;
parent:=self;
end;
with FFileList do
begin
top:=60;
left:=220;
//ffilelist.items:=('Bob');
parent:=self;
end;
with fLoadButton do
begin
top:=20;
left:=220;
caption:='Load';
parent:=self;
onclick:= myClick;
end;
end;
destructor TSearchAndLoad.destroy;
begin
fsearchEdit.free;
FFileDisplay.free;
FFileList.Free;
FLoadButton.Free;
inherited destroy;
end;
procedure Register;
begin
RegisterComponents('Samples', [TSearchAndLoad]);
end;
end.