دلفي 2009 - تعيين قيم الخصائص الافتراضية في مكونات دلفي المخصصة
-
20-09-2019 - |
سؤال
يجب أن يكون هذا بسيطًا جدًا ولكن لا يمكنني العثور على الإجابة الدقيقة التي أريدها.لدي تحكم دلفي مخصص يعتمد على TSpeedButton.أريد أن تكون خاصية التسمية التوضيحية لـ SpeedButton دائمًا "تعليقات" ولكني لا أريد تعيينها في وقت التشغيل، بل أريد تعيينها في المكون نفسه بحيث عندما أضعها في النموذج الخاص بي، يتم ملؤها بالفعل بهذا نص.أريد أيضًا ضبط ارتفاع الزر وعرضه ولكني أتخيل أن طريقة القيام بذلك ستكون هي نفسها المستخدمة في تعيين التسمية التوضيحية.
من أجل الاكتمال، إليك رمز المكون:
unit CustomSpeedButton;
interface
uses
SysUtils, Classes, Controls, Buttons;
type
TCustomSpeedButton = class(TSpeedButton)
private
FCommentText: string;
FCommentTitle: string;
procedure SetCommentText(const Value: string);
procedure SetCommentTitle(const Value: string);
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property CommentTitle: string read FCommentTitle write SetCommentTitle;
property CommentText: string read FCommentText write SetCommentText;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TCustomSpeedButton]);
end;
{ TCustomSpeedButton }
procedure TCustomSpeedButton.SetCommentText(const Value: string);
begin
FCommentText := Value;
end;
procedure TCustomSpeedButton.SetCommentTitle(const Value: string);
begin
FCommentTitle := Value;
end;
end.
المحلول
نظرًا لأنك أردت تنفيذ خاصية Caption بشكل صحيح، فإن إجابة Mason لن تعمل لأنه فاته شيء "csSetCaption"، ولن يعمل اقتراحه حول "الافتراضي" أيضًا لأن كلا من Caption وخصائصك هما من أنواع السلسلة.
فيما يلي الوحدات كما تريدها.
السلوك هو كما يلي:
- في البداية ستكون قيمة خاصية Caption هي "التعليقات"
- يمكن للمستخدمين تجاوز ذلك في وقت التصميم عن طريق تعيين قيمة جديدة
(إذا كنت لا تريد 2.، فأنت بحاجة إلى تعيين خاصية التسمية التوضيحية بطريقة محملة تم تجاوزها مثل ما ذكره كين؛ومع ذلك، لم يكن واضحًا من سؤالك ما إذا كنت تريد ذلك.إذا قمت بذلك، يرجى إعادة صياغة سؤالك.)
هذه هي الطريقة التي يعمل بها الكود.
بالنسبة لخصائص السلسلة، لا يمكنك التلميح إلى نظام التدفق لأي منها تقصير.ولكن يمكنك تعيين قيمة أولية لوقت التصميم في المُنشئ: التسمية التوضيحية := DefaultCustomSpeedButtonCaption;
بالنسبة لخاصية Caption، يجب عليك أيضًا تعطيل التعيين الافتراضي لخاصية Caption (وإلا سيحصل المكون الخاص بك تلقائيًا على تسمية توضيحية مثل 'CustomSpeedButton1').هذا الخط يفعل ذلك لك: ControlStyle := ControlStyle - [csSetCaption];
وأخيرًا، من الممارسات الجيدة تقسيم تسجيل المكونات الخاصة بك إلى وحدة منفصلة.يتيح لك ذلك الحصول على حزمة وقت التصميم التي تسجل مكوناتك في IDE وحزمة وقت التشغيل (أو عدم وجود حزمة على الإطلاق) لاستخدام مكوناتك في تطبيقاتك.
إذا كان لديك رمز مكون، فيمكنك تحميله في وحدة التسجيل أيضًا (نظرًا لأنه مطلوب فقط في وقت التصميم).
كتب راي كونوبكا كتابًا ممتازًا عن كتابة المكونات والذي لا يزال صالحًا للغاية: تطوير مكونات دلفي 3 مخصصةمثل العديد من كتب دلفي الجيدة، نفدت طبعته، لكن يمكنك ذلك طلب نسخة PDF على موقعه.
لست متأكدًا من الغرض من خصائص CommentTitle وCommentText، لذا احتفظت بهما في المصدر أدناه.
القائمة 1:المكون الفعلي
unit CustomSpeedButtonUnit;
interface
uses
SysUtils, Classes, Controls, Buttons;
const
DefaultCustomSpeedButtonCaption = 'Comments';
type
TCustomCustomSpeedButton = class(TSpeedButton)
strict private
FCommentText: string;
FCommentTitle: string;
strict protected
procedure SetCommentText(const Value: string); virtual;
procedure SetCommentTitle(const Value: string); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property CommentTitle: string read FCommentTitle write SetCommentTitle;
property CommentText: string read FCommentText write SetCommentText;
end;
TCustomSpeedButton = class(TCustomCustomSpeedButton)
published
// note you cannot use 'default' for string types; 'default' is only valid for ordinal ordinal, pointer or small set type
// [DCC Error] CustomSpeedButtonUnit.pas(29): E2146 Default values must be of ordinal, pointer or small set type
// property Caption default DefaultCustomSpeedButtonCaption;
property CommentTitle;
property CommentText;
end;
implementation
constructor TCustomCustomSpeedButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Caption := DefaultCustomSpeedButtonCaption;
ControlStyle := ControlStyle - [csSetCaption];
end;
destructor TCustomCustomSpeedButton.Destroy;
begin
inherited Destroy;
end;
procedure TCustomCustomSpeedButton.SetCommentText(const Value: string);
begin
FCommentText := Value;
end;
procedure TCustomCustomSpeedButton.SetCommentTitle(const Value: string);
begin
FCommentTitle := Value;
end;
end.
القائمة 2:تسجيل المكون
unit CustomSpeedButtonRegistrationUnit;
interface
procedure Register;
implementation
uses
CustomSpeedButtonUnit;
procedure Register;
begin
RegisterComponents('Standard', [TCustomSpeedButton]);
end;
end.
نصائح أخرى
وتحتاج إلى تعيين القيم الأصلية في البناء المكون.
وتحرير: أنت تحتاج أيضا إلى وضع إضافة ControlStyle := ControlStyle - [csSetCaption];
إلى منشئ
وEtherman: في حال كنت تريد أن يكون لها خاصية سلسلة مع القيمة الافتراضية وتريد حفظه إلى سوق دبي المالي - حتى لو كان فارغا - عليك أن تفعل ذلك بنفسك. لحسن الحظ، دلفي يمنحك طرق للقيام بذلك. نلقي نظرة على هذا الرمز:
type
TMyComp = class(TControl)
private
FMyStringProperty: string;
procedure WriteMyStringProperty(Writer: TWriter);
procedure DefineProperties(Filer: TFiler); override;
public
constructor Create(AOwner: TComponent); override;
published
property MyStringProperty: string read FMyStringProperty write FMyStringProperty stored False;
end;
implementation
constructor TMyComp.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMyStringProperty := 'my default value';
end;
procedure TMyComp.WriteMyStringProperty(Writer: TWriter);
begin
Writer.WriteString(FMyStringProperty);
end;
procedure TMyComp.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('MyStringProperty', nil, WriteMyStringProperty, True);
end;