?
Solved

Create a VCL property with 3 sub-properties

Posted on 2000-05-16
11
Medium Priority
?
265 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:ginsonic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 10

Expert Comment

by:Lischke
ID: 2814027
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
0
 
LVL 10

Accepted Solution

by:
ptmcomp earned 200 total points
ID: 2814974
That's one solution.

Another solution:

TMyClass= class(TPersistent);
private
  FColorHight: TMyType;
  ...
published
  property ColorHight: Integer read FColorHight write FColorHight;
  property ...
end;

TMyComponent
private
  FColors: TMyClass;
public
  constructor Create(aOwner: TComponent); override; // create here the Colors object (FColors:= TMyClass.Create)
  destructor; override; // and destroy it here
publish
  property Colors: TMyClass read FColors;
end;

Regards, ptm.
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 2814984
Lischke's solution works if MyColors is a component...
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!

 
LVL 3

Expert Comment

by:Stefaan
ID: 2816034
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
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2816264
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
0
 
LVL 9

Author Comment

by:ginsonic
ID: 2817034
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
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2817640
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
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2817647
Oops, forgot something:

procedure TMyColor.SetColorHigh(const Value: TColor);

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

Ciao, Mike
0
 
LVL 3

Expert Comment

by:Stefaan
ID: 2817687
Hi,

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


Stefaan
0
 
LVL 9

Author Comment

by:ginsonic
ID: 2822682
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 .
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2824645
Okey, dokey.

Ciao, Mike
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question