We help IT Professionals succeed at work.

Component help

Romans
Romans asked
on
You know when you drop and 2 or 3 ImageLists on a form, and they show up in the object inspector for a ListView - LargeImage List ... for an example ..

Well how do i go about doing this with a component that I am creating? How do I access all the properties, methods and event of the imagelist?

Can somebody provide me with a good example of a component that does this.

Thanks
Comment
Watch Question

Hi Romans,

Im not sure if I follow what you mean.  Do you want your component to be able to access the properties of a listview on a form, or do you want to create a listview descendant?

Stu

Commented:
Have a look at TypInfo unit which contains utility functions to access RTTI (run-time type information). Delphi generates RTTI automatically (unless you turn it off with a compiler directive) for all published properties of any TPersistent descendant.
HTH
TOndrej

Commented:
I think I know what you mean.

If you declare a property like this

property Images: TImageList read FImageList write FImageList;


In your code do this


if Assigned(Images) then
  Images.Whatever;


Here are some tips !!

1) When you refer to an object, always check for the object being destroyed;

(In your component)
procedure Notification(AComponent: TComponent; Operation: TOperation); override;

Then....
procedure TMyComponent.Notification(AComponent: TComponent; Operation: TOperation); override;
begin
  inherited;
  if (Operation = opRemove) and (AComponent = FImages) then Images := nil;
end;

2) You are only notified of components on the same form being destroyed unless you specifically ask the component to notify you.

procedure SetImages(Value: TImageList);
property Images: TImageList read FImages write SetImages;

Then...
procedure TMyComponent.SetImages(Value: TImageList);
begin
  if Assigned(Images) then Images.RemoveFreeNotification(Self);
  FImages := Value;
  if Assigned(Images) then Images.FreeNotification(Self);
end;

There are 2 component-writing articles on my web site www.HowToDoThings.com covering basics + properties of this sort.

http://www.howtodothings.com/showarticle.asp?article=310
and
http://www.howtodothings.com/showarticle.asp?article=320

Pete
====
http://www.HowToDoThings.com (Delphi articles)
http://www.Stuckindoors.com/delphi (Graphics, Sound, etc, open-source)
Commented:
Hi Romans.

In addition to excellent but incompleted cubud's comment I will show you how to react on internal changes of linked image list. To intercept the changes in a linked image list you must declare an object of type TChangeLink (see VCL source, imglist.pas or online help) - one changelink per imagelist. Further you have to attach event handler for TChangeLink.OnChange event. The last step is to inform linked image list that your component is interested in change notifications by calling RegisterChanges method of the image list passing a reference of your TChangeLink object as only parameter. This must be done each time the Images property gets changes (in SetImages method). Similarly, when you no longer Every time a linked image list changes, you call image list's UnRegisterChanges method passing the same reference to TChangeLink as used with RegisterChanges.


Here is the sample code:


TSomeComponent = class(TComponent)
private
  FImages: TCustomImageList;
  FImageChangeLink: TChangeLink;
  procedure SetImages(Value: TCustomImageList);
  procedure ImageListChanged(Sender: TObject);
public
  constructor Create(AOwner: TComponent); override;
  destructor Destroy; override;
published
  property Images: TCustomImageList read FImages write SetImages;
end;

{ TSomeComponent }

constructor TSomeComponent.Create(AOwner: TComponent);
begin
  inherited;
  FImageChangeLink := TChangeLink.Create;
  FImageChangeLink.OnChange := ImageListChanged;
end;

destructor TSomeComponent.Destroy;
begin
  FImageChangeLink.Free;
  inherited;
end;

procedure TSomeComponent.SetImages(Value: TCustomImageList);
begin
  if FImages <> Value then
  begin
    if Assigned(FImages) then
    begin
      FImages.UnRegisterChanges(FImageChangeLink);
      FImages.RemoveFreeNotification(Self);
    end;

    FImages := Value;

    if Assigned(FImages) then
    begin
      FImages.RegisterChanges(FImageChangeLink);
      FImages.FreeNotification(Self);
    end;
  end;
end;

procedure TSomeComponent.ImageListChanged(Sender: TObject);
begin
  // react on image list change...
end;


Hope this helps, Ivo.

Commented:
Yep, Ivo beat me on that one :)

But I still recommend reading those articles, especially as now part 3 has been added too !

http://www.howtodothings.com/showarticle.asp?article=327

Pete
====
http://www.HowToDoThings.com (Delphi articles)
http://www.Stuckindoors.com/delphi (Graphics, Sound, etc, open-source)
Romans:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.