Link to home
Start Free TrialLog in
Avatar of wildzero
wildzero

asked on

Component - Create form now change items

Hi there,

I am writing a custom component which opens up a new form and on this form is displayed certain information
I have most of it going good however I can't seem to get the component to comunicate with the form...
Let me explain, here is my code

type
  TShareReg = class(TComponent)
  private
    { Private declarations }
    FLocalFile  : string;
    FMyForm    : TForm;
  protected
    { Protected declarations }
  public
    { Public declarations }
    Constructor create(aowner:tcomponent); override;
    Destructor destroy; override;
    Function Execute : boolean; virtual;
  published
    { Published declarations }
  end;

procedure Register;

implementation

uses frmMyForm;

procedure Register;
begin
  RegisterComponents('OwnComp', [TShareReg]);
end;
//==============================================================================

{ TShareReg }
constructor TShareReg.create(aowner: tcomponent);
begin
  inherited create(aowner);
 
  FMyForm  :=  TfrmMyFrm.create(self);
end;
//==============================================================================

Now I know the form is getting created, but what I want to do is set the value of FLocalFile  to a TLabel on the FMyForm.
So I tried doing
FMyForm.Label1.caption := FLocalFile;

But I get undeclaired identifiyer.
So I thought I could do that on the OnCreate event for FMyForm... but that fails also.

What am I missing
Avatar of kenpem
kenpem
Flag of United Kingdom of Great Britain and Northern Ireland image

hmmm....is Caption a private property, maybe? Even so, within the OnCreate of the MyForm should work (you did first call inherited, right?).
Avatar of wildzero
wildzero

ASKER

Explain :-)

The OnCreate of MyForm I should call Inherited? The MyForm is just a TForm which has componets etc on it.... wouldn't think I would need to call inherited...
ASKER CERTIFIED SOLUTION
Avatar of TName
TName

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
Have you tried using frmMyFrm (wich probably is declared as TfrmMyFrm in frmMyForm.pas):

frmMyForm.Label1.Caption;
I mean
frmMyForm.Label1.Caption:= FLocalFile;
one "o" too many:
frmMyFrm.Label1.Caption:= FLocalFile;

should have c&p-ed instead of typing... ;)
Testing now - so far looks good. Standby.
Drat, for some reason I am now getting an access violation error in rtl70.bpl...
Get this when I open the project which has my component in it....
frmMyFrm.Label1.Caption:= FLocalFile;
that didn't work (caused that above error)

TfrmMyFrm(FMyForm).Label1.caption := FLocalFile;
that worked :-)
Thanks guys!
Thanks for the points! ;)

Your initial code didn't work because FMyForm  is declared as TForm instead of TfrmMyFrm.
If you declare it like this:   FMyForm:TfrmMyFrm;  *and* move the   "uses frmMyForm;"  to the top (beginning of interface section) it should work without the need to typecast it.


>frmMyFrm.Label1.Caption:= FLocalFile;
>that didn't work (caused that above error)
Did you create frmMyFrm without redeclaring it (it's already declared in frmMyForm.pas)?
I just had to try it, and this works for me (I simply created a TShareReg instance in a project):

constructor TShareReg.create(aOwner: tcomponent);
begin
inherited create(aOwner);
   FLocalFile:='SomeFileName';
   frmMyFrm:=TfrmMyFrm.create(self);
   frmMyFrm.Show;  //just to test it
   frmMyFrm.Label1.Caption:=FLocalFile;
end;