Link to home
Start Free TrialLog in
Avatar of ginsonic
ginsonicFlag for Romania

asked on

Create a VCL property with 3 sub-properties

How can I create a property into my component that can be displayed in
ObjectInspector with +MyProperty ?
When I click on it to roll down some new properties .
Alike Anchors or Font or ....

I need to add a property named MyColors with 3 sub-properties :
ColorHight
ColorLow
ColorBetween .

A code sample for do that ?

Regards,
Nick
Avatar of Lischke
Lischke

Hi Nick,

usually subcomponents use an already defined property editor class, like TFont for which automatically necessary properties for the object inspector are declared. For new subcomponents you need to write an own property editor. The easiest way to do that is to derive it from TClassProperty:

  TGLTextureProperty = class(TClassProperty)
  protected
    function GetAttributes: TPropertyAttributes; override;
  end;

The GetAttributes function returns some flags which describe how the object inspector should handle the property:

function TGLTextureProperty.GetAttributes: TPropertyAttributes;

begin
  Result := [paSubProperties];
end;

You could also include paDialog to create the little button with the 2 points (like the one for TFont).

Finally you must register your new property editor with the particular property:

RegisterPropertyEditor(TypeInfo(TGLTexture), TGLMaterial, '', TGLTextureProperty);

The example TGLMaterial is here to limit the use of the new editor to all TGLTexture properties of the class TGLMaterial. If this would be nil then the editor could be used for any class where a property is of type TGLTexture. This way you can redefine a property editor for already existing classes (I did it with wide strings and have now for ever wide string property (even for already existing components) my new editor.

Ciao, Mike
ASKER CERTIFIED SOLUTION
Avatar of ptmcomp
ptmcomp
Flag of Switzerland image

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
Lischke's solution works if MyColors is a component...
Hi,

IMHO the best way is to make a component of it, which descends from TPersistent like ptmcomp said.

Most of the time I do it that way, and it works perfectly.

Stefaan
My solution works also with TPersistent. The point, though, is that no special property editor is needed if only the subproperties should be shown in the object inspector. How could I miss that? So it just turns out to be ptmcomp said:

1) create your MyColor class

TMyColor = class(TPersistent)
private
  FColorHigh,
  FColorLow,
  FColorBetween: TColor;
published
  property ColorHigh: TColor read FColorHigh write FColorHigh;
  property ColorLow: TColor read FColorLow write FColorLow;
  property ColorBetween: TColor read FColorBetween write FColorBetween;
end;


2) create the container component

TMyComponent = class(TComponent)
private
  FMyColor: TMyColor;
  procedure SetMyColor(const Value: TMyColor);
public
  constructor Create(AOwner: TComponent); override;
  destructor Destroy; override;
published
  property MyColor: TMyColor read FMyColor write SetMyColor;
end;

TMyComponent.SetMyColor(const Value: TMyColor);
begin
  FMyColor.Assign(Value); // or directly set the values
end;

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FMyColor := TMyColor.Create;
end;

destructor TMyComponent.Destroy;
begin
  FMyColor.Free;
  inherited;
end;

Ciao, Mike
Avatar of ginsonic

ASKER

I can't use the values or colors .
I can paint first time , but when I change , for example , the ColorHight the component don't repaint .

I try it the SetMyColor from Mike code ,  but don't work :(
I used Invalidate , Paint ... don't work .
If I put over my component a form and then I move it ( force the paint ) the component is painted OK .

Comments ?

Regards,
Nick
Also this is no big problem. In order to have the component reflect changes of its properties you need set methods instead directly writing to the internal variables:

TMyColor = class(TPersistent)
private
  FColorHigh,
  FColorLow,
  FColorBetween: TColor;
  FOwner: TControl;
  procedure SetColorHigh(const Value: TColor);
public
  constructor Create(AOwner: TControl);
published
  property ColorHigh: TColor read FColorHigh write SetColorHigh;
end;

constructor TMyColor.Create(AOwner: TControl);

begin
  inherited;
  FOwner = AOwner;
end;

procedure TMyColor.SetColorHigh(const Value: TColor);

begin
  FOwner.Invalidate;
end;

The owner for the TMyColor class is of course TMyComponent which should pass itself as owner while creating FMyColor:

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FMyColor := TMyColor.Create(Self);
end;

Ciao, Mike
Oops, forgot something:

procedure TMyColor.SetColorHigh(const Value: TColor);

begin
  if FColorHigh <> Value then
  begin
    FColorHigh := Value;
    FOwner.Invalidate;
  end;
end;

Ciao, Mike
Hi,

Can you show us the code please, so we can give you some additional help >


Stefaan
Work now .

ptmcomp give me first the right answer , but Lischke complete it .....

I will give these points to ptmcomp and I will put a new question for Lischke . Another 50 points .

Lischke please let a comment if you read that message to give you another 50 points .

THANKS TO ALL .
Okey, dokey.

Ciao, Mike