how to add a property to a component that will reflect on the object inspector
in De开发者_如何学Clphi 7 , when adding a propery to an object, how is it possible to see that property in the object inspector?
Make the property published
. For instance,
private
FMyProperty: integer;
published
property MyProperty: integer read FMyProperty write FMyProperty;
Often, you need to repaint the control (or do some other processing) when a property is changed. Then you can do
private
FMyProperty: integer;
procedure SetMyProperty(MyProperty: integer);
published
property MyProperty: integer read FMyProperty write SetMyProperty;
...
procedure TMyControl.SetMyProperty(MyProperty: integer);
begin
if FMyProperty <> MyProperty then
begin
FMyProperty := MyProperty;
Invalidate; // for example
end;
end;
Add that property to the published section, it will make it appear on the Object Inspector, like this:
TMyComponent = class(TComponent)
...
published
property MyProperty: string read FMyProperty write SetMyProperty;
From the docs:
Properties declared in a published section of the component's class declaration are editable in the Object Inspector at design time.
Don't forget that the Component needs to get registered within Delphi (preferable in a Design Time Package) or you you won't see anything in the Object Inspector at all !!!
I mean ... I can create a new TPanel descendant called TMyPanel and add a new Published property to it :
type
TPanel1 = class(TPanel)
private
FMyName: String;
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property MyName : String read FMyName write FMyName;
end;
but that property won't get displayed in the Object Inspector if you havn't registered the new class using RegisterComponent :
procedure Register;
begin
RegisterComponents('Samples', [TPanel1]);
end;
Just to be complete :-)
精彩评论