Link to home
Start Free TrialLog in
Avatar of APS NZ
APS NZFlag for New Zealand

asked on

Adding a visual component to a non visual one

Hi

I didn't get an answer to my last question, so I'm trying again from a different approach.  How can I add a visual component to a non visual component and make it visible at both design time and runtime?

I am talking about adding a TGauge to a TReport.

I have done the following:

type
  TJRptMeter = class(TReport)
  private
    { Private declarations }
    FGauge: TGauge;

etc etc...

constructor TJRptMeter.Create(AOwner: TComponent);
begin
     inherited Create(AOwner);
     FGauge := TGauge.Create(Self);
     FGauge.Parent := Self;
     FGauge.Visible := True;
     FGauge.Height := 25;
     FGauge.ForeColor := clRed;
end;

The line FGauge.Parent := Self produces the error "Type Mismatch".  Also the Gauge does not become visible even at runtime.  Any ideas about what I need to do???
Avatar of JimBob091197
JimBob091197

Hi

As you probably know, TReport inherits from TComponent.  That's why you can't assign the gauge's parent to the report.  A visible control's (the gauge) parent must be a visible control, not a TComponent.

Try setting FGauge.Parent := Self.Owner;

JB
Avatar of APS NZ

ASKER

Hi JimBob - thanks for the reply.  I tried that, but I still get the Type Mismatch error.  Any other ideas??
Try type-casting it to a TWinControl.

E.g.  FGauge.Parent := TWinControl(Self.Owner);

JB
Avatar of APS NZ

ASKER

Thanks again JimBob

That one works OK, but it GPFs in Complib.dcl when I close the form.  Also is there a way to make it move when I move the TReport it's attached to.  TReport doesn't have Top and Left properties.  Or can I drag it (the TGauge part)?
Mmm...  Regarding the GPF, are you freeing the gauge in your report's destructor?  Try setting FGauge.Parent := nil before freeing the gauge.

Regarding the moving when you move the TReport, I don't have anything to suggest, except that you may only want to create the gauge at run-time (check "if (csDesigning in ComponentState) then ...").

The other alternative is not to have the gauge created & owned by the report, but to write a custom gauge which the report can link to.  I.e. have a property on the report called "Gauge" of type TGauge, which the report can use if the report's Gauge property <> nil.

JB
Avatar of APS NZ

ASKER

Hi JimBob

I've spent some time experimenting after your last comments, and I've finally got things working.  I've had to compromise on some of it, but the main thing is it will do what I need.  I want to give you the points, so if you'd like to convert to an answer I'll gladly pass them on.

Thanks again for all your help.

JD
ASKER CERTIFIED SOLUTION
Avatar of JimBob091197
JimBob091197

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
Avatar of APS NZ

ASKER

Hi JimBob - yes, I did use the TWinControl method. Thanks again