dluedi
asked on
functions
Hello!
I tried to program following function in my prog:
function pic(where:TCustomForm):boo lean;
begin
TForm(where).picture.loadf romfile('c ool.bmp');
TForm(where).button1.enabl ed:=false;
//TCustomForm(where) doesn't work either:
{...}
end;
But it doesn't work!
Can somebody help me??
:)dluedi
I tried to program following function in my prog:
function pic(where:TCustomForm):boo
begin
TForm(where).picture.loadf
TForm(where).button1.enabl
//TCustomForm(where) doesn't work either:
{...}
end;
But it doesn't work!
Can somebody help me??
:)dluedi
ASKER
I think you're on the right way but I forgot to tell you that I want to use the "where" var as indicator to show on which forms the pictures should be loaded: That's why I can't use TForm1(where) and then TForm2(where) because "where" should either say Form1 or Form2....
function pic(where:TCustomForm):boo lean;
begin
TForm(where).picture1.Load FromFile(' cool.bmp') ;
//here where should tell which form
TForm(where).picture2.Load FromFile(' blabla.bmp ');
//here too
{...}
end;
so if I use it like this...
pic(Form1);
the pictures one and two should change on Form1
and if I use it lik this...
pic(Form2);
then the pictures should change from Form2
:) dluedi
If you got lot work for this question then you'll get more points
function pic(where:TCustomForm):boo
begin
TForm(where).picture1.Load
//here where should tell which form
TForm(where).picture2.Load
//here too
{...}
end;
so if I use it like this...
pic(Form1);
the pictures one and two should change on Form1
and if I use it lik this...
pic(Form2);
then the pictures should change from Form2
:) dluedi
If you got lot work for this question then you'll get more points
Two possibilities:
1) instead of descending TForm1 & TForm2 from TCustomForm, descend them from something that has a TPicture on it. IE create a a different form type then descend both TForm1 & TForm2 from that.
2) name the TPicture's on both forms the same and then try something like this:
with where.findComponent('pictu re') as TPicture do loadFromFile('cool.bmp');
GL
Mike
1) instead of descending TForm1 & TForm2 from TCustomForm, descend them from something that has a TPicture on it. IE create a a different form type then descend both TForm1 & TForm2 from that.
2) name the TPicture's on both forms the same and then try something like this:
with where.findComponent('pictu
GL
Mike
ok, in that case, create a common ancestor for the two forms, say TAncestorForm. Create a form with all the required common components on it, name it AncestorForm and put it in the repository. (rt click and say add to repository ...)
then file, new... and choose AncestorForm from Project tab to create Form1, Form2, etc.
then
type
TForm1=Class(TAncestorForm )
{...}
end;
function pic(what:TAncestorForm):Bo olean;
begin
What.Picture1.LoadFromFile ('Cool.bmp ');
{...}
end;
HTH
Chris
then file, new... and choose AncestorForm from Project tab to create Form1, Form2, etc.
then
type
TForm1=Class(TAncestorForm
{...}
end;
function pic(what:TAncestorForm):Bo
begin
What.Picture1.LoadFromFile
{...}
end;
HTH
Chris
Hi dluedi,
another way is to check "where" variable to know what is it represents.
-----
Igor.
function pic(Where: TCustomFomr): Boolean;
begin
if Where is TForm1 then
begin
TForm1(Where).Picture1.Loa dFromFile. ..
....
end
else
if Where is TForm2 then
begin
TForm2(Where).Picture20.Lo adFromFile ....
....
end
else
............
another way is to check "where" variable to know what is it represents.
-----
Igor.
function pic(Where: TCustomFomr): Boolean;
begin
if Where is TForm1 then
begin
TForm1(Where).Picture1.Loa
....
end
else
if Where is TForm2 then
begin
TForm2(Where).Picture20.Lo
....
end
else
............
ASKER
Sorry that it took so long, I actually don't understand what you mean with ancestorform.
ITugay your comment would be good, but the where var should already say TForm1!
You know I tried to make this question easier, because actually I've got three forms. Each of this forms has to do the same very long code {100+ lines}, that's why I want to make it to one only procedure.
(The application was in the beginning 100 megs big, now I'm at 11 megs, and I want to make the code more compact)
I think I'll post a new link, because this question is quite old. By the way Chris Farrow, please don't always propose an answer, because fewer experts help then, thank you!
:) dluedi
ITugay your comment would be good, but the where var should already say TForm1!
You know I tried to make this question easier, because actually I've got three forms. Each of this forms has to do the same very long code {100+ lines}, that's why I want to make it to one only procedure.
(The application was in the beginning 100 megs big, now I'm at 11 megs, and I want to make the code more compact)
I think I'll post a new link, because this question is quite old. By the way Chris Farrow, please don't always propose an answer, because fewer experts help then, thank you!
:) dluedi
ASKER
Maybe this helps, the three forms all contain the same components! So if Form1 has an EditBox Form2 and Form3 have one too.
If I understand what you're trying to do, you have three different forms with identical component names, which you
are trying to cast to the same thing to save code:
I haven't tested anything here, but try this:
function pic(where:TCustomForm):boo lean;
begin
(where as TForm1).picture.loadfromfi le('cool.b mp);
end;
I hae my doubts, though, when you try to pass form2 which may be structurally identical to form 1, but represented as a different object. It might work, though, rather than the direct cast you are trying. You could also try:
if (where is Tform1) then
(where as Tform1)...
but you'd need a cast for each form, so it wouldn't save you much. If you are using Delphi 5, this sounds like a good use for frames - make each form from the same frame type, then you can access them identically.
are trying to cast to the same thing to save code:
I haven't tested anything here, but try this:
function pic(where:TCustomForm):boo
begin
(where as TForm1).picture.loadfromfi
end;
I hae my doubts, though, when you try to pass form2 which may be structurally identical to form 1, but represented as a different object. It might work, though, rather than the direct cast you are trying. You could also try:
if (where is Tform1) then
(where as Tform1)...
but you'd need a cast for each form, so it wouldn't save you much. If you are using Delphi 5, this sounds like a good use for frames - make each form from the same frame type, then you can access them identically.
This one only work if the same components
of each form have the same name
function pic(where : TCustomForm) : boolean;
begin
TImage(where.findComponent ('Image1') ).Picture. loadFromFi le('D:\tes tbild2.bmp ');
TButton(where.findComponen t('Button1 ')).Enable d := FALSE;
{...}
end;
of each form have the same name
function pic(where : TCustomForm) : boolean;
begin
TImage(where.findComponent
TButton(where.findComponen
{...}
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for example:
type
TForm1=class(TForm)
picture:TImage;
Button1:TButton;
{...}
end;
function pic(where:TCustomForm):boo
begin
TForm1(where).picture.Load
TForm1(where).Button1.Enab
{...}
end;