如何将两个属性设置为节点并在设计模式下更改标题?

问题描述 投票:0回答:1

我正在尝试创建一个名为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.

一切都工作正常,但我想在一个节点上制作EnableCaptionDisableCaption,因为TToggleSwitchStateCaptions属性,当我更改标题时,它也会在CheckBox中改变它。

我试着在Invalidate;SetEnableCaption程序中调用SetDisbleCaption,但这不起作用。

我怎样才能做到这一点?

delphi delphi-10-seattle
1个回答
1
投票

说实话,我一开始不想回答这个问题,因为你已经在SO的一个问题上得到了答案

Create a button that accepts .PNG images as Glyph

第一类确切地说是TPersistent暴露了TNCRSpeedButton中的Glyph坐标。

我写这篇是因为我说我希望你从中受益匪浅。我可以看到你没有。

所以这是您解决问题的方法,我们非常欢迎您询问有关如何实施的问题。

unit UnitName;;

interface
   uses
    System.Classes, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Controls;

type
  TCheckEditCaptions = class(TPersistent)
  private
    FDisableCaption: TCaption;
    FEnableCaption: TCaption;
    FOnChange: TNotifyEvent;
    function GetDisableCaption: TCaption;
    function GetEnableCaption: TCaption;
    procedure SetDisableCaption(const Value: TCaption);
    procedure SetEnableCaption(const Value: TCaption);
  public
    procedure Assign(aValue: TPersistent); override;
  published
    property EnableCaption: TCaption read GetEnableCaption write SetEnableCaption;
    property DisableCaption: TCaption read GetDisableCaption write SetDisableCaption;
    property OnChange: TNotifyEvent read FOnChange write FOnChange;
  end;

TCheckEdit = class (TCustomControl)
  private
    FCheckBox : TCheckBox;
    FEdit : TEdit;
    FCheckEditCaptions: TCheckEditCaptions;
    procedure SetIsActive(const Value: Boolean);
    function GetIsActive : Boolean;
    procedure ChBoxOnClick (Sender : TObject);
    procedure CheckEditCaptionsChanged(Sender : TObject);
    procedure SetCheckEditCaptions(const Value: TCheckEditCaptions);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property IsActive : Boolean read GetIsActive write SetIsActive default False;
    property CheckEditCaptions : TCheckEditCaptions read FCheckEditCaptions write SetCheckEditCaptions;
    property OnClick;
end;
procedure register;

implementation

 procedure register;
 begin
   RegisterComponents('Samples', [TCheckEdit]);
 end;

{ TCheckEdit }

procedure TCheckEdit.ChBoxOnClick(Sender: TObject);
begin
  IsActive := FCheckBox.Checked;
end;

procedure TCheckEdit.CheckEditCaptionsChanged(Sender: TObject);
begin
  SetIsActive(GetIsActive);
end;

constructor TCheckEdit.Create(AOwner: TComponent);
begin
  inherited;

  FCheckBox := TCheckBox.Create(Self);
  FCheckBox.Parent := Self;
  FCheckBox.Align := alTop;
  FCheckBox.OnClick := ChBoxOnClick;

  FCheckEditCaptions := TCheckEditCaptions.Create;
  FCheckEditCaptions.FDisableCaption := 'Disabled';
  FCheckEditCaptions.FEnableCaption := 'Enabled';
  FCheckEditCaptions.OnChange := CheckEditCaptionsChanged;

  FCheckBox.Caption := CheckEditCaptions.DisableCaption;

  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;
  FCheckEditCaptions.Free;
  inherited;
end;

function TCheckEdit.GetIsActive: Boolean;
begin
  Result := FCheckBox.Checked ;
end;

procedure TCheckEdit.SetCheckEditCaptions(const Value: TCheckEditCaptions);
begin
  FCheckEditCaptions.Assign(Value);
end;

procedure TCheckEdit.SetIsActive(const Value: Boolean);
begin
  FCheckBox.Checked := Value;
  FEdit.Enabled := Value;
  if Value then
    FCheckBox.Caption := CheckEditCaptions.EnableCaption
  else
    FCheckBox.Caption := CheckEditCaptions.DisableCaption;
end;
{ TCheckEditCaptions }

procedure TCheckEditCaptions.Assign(aValue: TPersistent);
begin
  if aValue is TCheckEditCaptions then begin
    FEnableCaption := TCheckEditCaptions(aValue).FEnableCaption;
    FEnableCaption := TCheckEditCaptions(aValue).FDisableCaption;
    if Assigned(FOnChange) then
       FOnChange(self);
  end else
    inherited;
end;

function TCheckEditCaptions.GetDisableCaption: TCaption;
begin
  result := FDisableCaption;
end;

function TCheckEditCaptions.GetEnableCaption: TCaption;
begin
  result := FEnableCaption;
end;

procedure TCheckEditCaptions.SetDisableCaption(const Value: TCaption);
begin
  FDisableCaption := Value;
  if Assigned(FOnChange) then
       FOnChange(self);
end;

procedure TCheckEditCaptions.SetEnableCaption(const Value: TCaption);
begin
  FEnableCaption := Value;
  if Assigned(FOnChange) then
       FOnChange(self);
end;

end.
© www.soinside.com 2019 - 2024. All rights reserved.