Link to home
Start Free TrialLog in
Avatar of dluedi
dluedi

asked on

functions

Hello!
I tried to program following function in my prog:

function pic(where:TCustomForm):boolean;
begin
TForm(where).picture.loadfromfile('cool.bmp');
TForm(where).button1.enabled:=false;
//TCustomForm(where) doesn't work either:
{...}
end;


But it doesn't work!
Can somebody help me??
:)dluedi

Avatar of ChrisFarrow
ChrisFarrow

you need to cast where as your form type.

for example:

type
  TForm1=class(TForm)
    picture:TImage;
    Button1:TButton;
    {...}
  end;


function pic(where:TCustomForm):boolean;
begin
  TForm1(where).picture.LoadFromFile('cool.bmp');
  TForm1(where).Button1.Enabled:=False;
  {...}
end;
Avatar of 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):boolean;
begin
TForm(where).picture1.LoadFromFile('cool.bmp');
//here where should tell which form
TForm(where).picture2.LoadFromFile('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
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('picture') as TPicture do loadFromFile('cool.bmp');

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):Boolean;
begin
  What.Picture1.LoadFromFile('Cool.bmp');
  {...}
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.LoadFromFile...
    ....
  end
  else
  if Where is TForm2 then
  begin
    TForm2(Where).Picture20.LoadFromFile....
    ....
  end
  else
  ............    
Avatar of dluedi

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
Avatar of 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):boolean;
begin
     (where as TForm1).picture.loadfromfile('cool.bmp);
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.loadFromFile('D:\testbild2.bmp');
 TButton(where.findComponent('Button1')).Enabled := FALSE;
 {...}
end;
ASKER CERTIFIED SOLUTION
Avatar of edsteele
edsteele

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial