Search code examples
delphidelphi-10-seattle

How to set two properties as a node and change the caption in design mode?


I'm trying to create a new component named CheckEdit as the following:

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.

Everything is working fine, but I want to make EnableCaption and DisableCaption in one node as TToggleSwitch have StateCaptions property, and when I change the caption it will change it in the CheckBox too.

I try to call Invalidate; in SetEnableCaption and SetDisbleCaption procedures, but that does not work.

How can I do that?


Solution

  • To be honest with you, I did not want to answer this question at first because you already have the answer in one of your questions here on SO

    Create a button that accepts .PNG images as Glyph

    the first class to be exact it is a TPersistent that exposed the Glyph coordinates in the TNCRSpeedButton.

    I'm writing this because I said I hope you benefit well from it. and I can see you did not.

    So this is your solution to your problem and you are more than welcome to ask any thing about how it is implemented.

    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.