【发布时间】:2017-12-31 14:41:06
【问题描述】:
我正在尝试创建一个名为 CheckEdit 的新组件,如下所示:
unit UnitName;
interface
uses
System.Classes, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Controls;
type
TCheckEdit = class (TCustomControl)
private
FCheckBox : TCheckBox;
FEdit : TEdit;
FEnableCaption: TCaption;
FDisbleCaption: TCaption;
procedure SetIsActive(const Value: Boolean);
function GetIsActive : Boolean;
procedure ChBoxOnClick (Sender : TObject);
procedure SetDisbleCaption(const Value: TCaption);
procedure SetEnableCaption(const Value: TCaption);
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property IsActive : Boolean read GetIsActive write SetIsActive default False;
property EnableCaption : TCaption read FEnableCaption write SetEnableCaption;
property DisbleCaption : TCaption read FDisbleCaption write SetDisbleCaption;
property OnClick;
end;
procedure register;
implementation
procedure register;
begin
RegisterComponents('Standard', [TCheckEdit]);
end;
{ TCheckEdit }
procedure TCheckEdit.ChBoxOnClick(Sender: TObject);
begin
if FCheckBox.Checked then
IsActive := True
else
IsActive:= False;
end;
constructor TCheckEdit.Create(AOwner: TComponent);
begin
inherited;
FCheckBox := TCheckBox.Create(Self);
FCheckBox.Parent := Self;
FCheckBox.Align := alTop;
FCheckBox.Caption := Self.Name;
FCheckBox.OnClick := ChBoxOnClick;
FDisbleCaption := 'Disabled';
FEnableCaption := 'Enabled';
FCheckBox.Caption := FDisbleCaption;
FEdit := TEdit.Create(Self);
FEdit.Parent := Self;
FEdit.Align := alTop;
FEdit.Enabled := False;
Self.Height := 40;
Self.Width := 185;
Self.AutoSize := True;
end;
destructor TCheckEdit.Destroy;
begin
FEdit.Free;
FCheckBox.Free;
inherited;
end;
function TCheckEdit.GetIsActive: Boolean;
begin
if FCheckBox.Checked then
Result := True
else
Result := False;
end;
procedure TCheckEdit.SetDisbleCaption(const Value: TCaption);
begin
FDisbleCaption := Value;
end;
procedure TCheckEdit.SetEnableCaption(const Value: TCaption);
begin
FEnableCaption := Value;
end;
procedure TCheckEdit.SetIsActive(const Value: Boolean);
begin
FCheckBox.Checked := Value;
case Value of
True :
begin
FEdit.Enabled := True;
FCheckBox.Caption := FEnableCaption;
end;
False :
begin
FEdit.Enabled := False;
FCheckBox.Caption := FDisbleCaption;
end;
end;
end;
end.
一切正常,但我想在一个节点中创建EnableCaption 和DisableCaption,因为TToggleSwitch 具有StateCaptions 属性,当我更改标题时,它也会在CheckBox 中更改它。
我尝试在SetEnableCaption 和SetDisbleCaption 程序中调用Invalidate;,但这不起作用。
我该怎么做?
【问题讨论】:
-
您可以将它们分组到
TPersistent已发布的课程中(由于在使用您的组件时可能进行分配而持续存在)。这是一种常见的做法。 -
@Victoria 你的意思是
TToggleSwitchStateCaptions? -
是的,例如。 VCL 中有很多示例(因为我的内存服务器,例如
TDBGrid具有Options属性)。 -
@Victoria 好的,我会尝试这样做,标题呢?当我更改
DisableCaption时,cackbox 的标题不会更改。