Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Creating Visual Components at run time

I am designing a component derived from TPanel, I wont to have an image on it (my own image component).

This is the code so far...

unit ImageContainer;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls,U_ImageExtended;

type
  TImageContainer = class(TPanel)
  Procedure SetImage(Value : TImageExtended);
  private
  FImage : TImageExtended;
    { Private declarations }
  protected
    { Protected declarations }
  public
  constructor Create(AOwner: TComponent); override;
    { Public declarations }
  Property Image : TImageExtended Read FImage Write SetImage;
  published

    { Published declarations }
  end;

procedure Register;

implementation


constructor TImageContainer.Create(AOwner: TComponent);
Begin
    Inherited Create(AOwner);
end;

Procedure TImageContainer.SetImage(Value : TImageExtended);
Begin
     FImage := Value;
end;

procedure Register;
begin
  RegisterComponents('My Components', [TImageContainer]);
end;

end.

When I try to put this on a form and execute any methods related to the Image I get a 'Read of address 'FFFFFF' message. The image is obviously not correctly created.
How can I create it?

Want a fast answer thereby High points.
0
MrBig
Asked:
MrBig
1 Solution
 
AleqCommented:
constructor TImageContainer.Create(AOwner: TObject);
begin
  inherited;
  FImage:=TImage.Create(Self);
  FImage.Parent:=Self; // if this makes problems, put it into CreateWND method of this class
end;

destructor TImageContainter.Destroy;
begin
  FImage.Free;
  inherited;
end;

Procedure TImageContainer.SetImage(Value : TImageExtended);
Begin
//     FImage := Value;
  FImage.Assign(Value);
end;

Hope, that's what you're asking for.



0
 
PeterLarsenCommented:
Hi,

First, i dont know what TImageExtended is...

You need to create a TImage in your constructor - like this :

Type
  TImageExtended = TImage;

FImage : TImageExtended;
...
constructor TImageContainer.Create(AOwner: TComponent);
Begin
 Inherited Create(AOwner);
 FImage:=TImageExtended.create(self);
end;

I also hink you should use Assign :

Procedure TImageContainer.SetImage(Value : TImageExtended);
Begin
 FImage.Assign := Value;
end;

I have not tried this sample, but i think this is how you should write it.

Good luck
Peter
0
 
PeterLarsenCommented:
Hmm, Sorry Aleq,
You where there first, but i did not see your comment.
Peter :-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fulvio_brasilCommented:
Hi MrBig,

Well I tried to compile your code, but you use your own TImageExtended, so I can't help you so much.

Anyway I think that you could use:
constructor TImageContainer.Create(AOwner: TComponent);
Begin
  Inherited Create(AOwner);
  FImage := TImageExtended.Create;
{or, depending you TImageExtended}
  FImage := TImageExtended.Create(AOwner);
end;

and put the line:
  Property Image : TImageExtended Read FImage Write SetImage;

after then published section of the class

0
 
PeterLarsenCommented:
PeterLarsen changed the proposed answer to a comment
0
 
MrBigAuthor Commented:
Hey u all, thanks for the quick answers, you all did a good job at it but Aleq beat you to the points, it's all yours aleq, and thax again!!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now