Ver Mensaje Individual
  #1  
Antiguo 01-11-2022
Avatar de José Luis Garcí
[José Luis Garcí] José Luis Garcí is offline
Miembro Premium
 
Registrado: may 2003
Ubicación: Las Palmas de G.C.
Posts: 1.372
Reputación: 23
José Luis Garcí Va camino a la fama
Problema con el componente, se activa al cambiar (DataSource.State)

Buenos días compañeros, tengo un problema con este componente, que es el siguiente, cuando cambia el State de este TDBToggleSwitch, me activa directamente el Datasource.State DsEdit y no se como remediarlo, como siempre espero a vuestras indicaciones y os doy mil gracias como siempre.

Código Delphi [-]
unit DBToggleSwitch;

interface

uses
  System.SysUtils, System.Classes, Vcl.Controls, Vcl.WinXCtrls, VCL.DBCtrls, dATA.DB;

type
  TDBToggleSwitch = class(TToggleSwitch)
  private
    { Private declarations }
    FDataLink : TFieldDataLink;
    FReadOnly : Boolean;
    Function GetDataField:string;
    Procedure SetDataField(const Value:string);
    Function GetDataSource:TDataSource;
    procedure SetDataSource(Value:TDataSource);
    Procedure DataChange(Sender:TObject);
  protected
    { Protected declarations }
     Procedure Notification(AComponent:TComponent;Operation:TOperation); override;
     Procedure Click; override;
  public
    { Public declarations }
    Constructor Create(AOwner : TComponent); override;
    Destructor Destroy; override;
  published
    { Published declarations }
     Property ReadOnly : Boolean read FReadOnly write FReadOnly default False;
     Property DataField : String read GetDataField write SetDataField;
     Property DataSource : TDataSource read GetDataSource write SetDataSource;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TDBToggleSwitch]);
end;

{ TDBToggleSwitch1 }

procedure TDBToggleSwitch.Click;
begin
   if (csDesigning in ComponentState) or FReadOnly or (FDataLink=nil) or (not fdatalink.Edit) then exit
   else
   begin
      if DataSource.State IN [dsEdit, dsInsert] then
      BEGIN
          if State=tssOff then DataSource.DataSet.FieldByName(DataField).Value:=stateCaptions.CaptionOff
                          else DataSource.DataSet.FieldByName(DataField).Value:=stateCaptions.CaptionOn;
        inherited;
        FDataLink.Modified;
      end;
   END;
end;

constructor TDBToggleSwitch.Create(AOwner: TComponent);
begin
    inherited;
    FReadOnly:=False;
    FDataLink:=TFieldDataLink.Create;
    FDataLink.OnDataChange:=DataChange;
end;

procedure TDBToggleSwitch.DataChange(Sender: TObject);
begin
  if (DataSource.DataSet.FieldByName(DataField).Value=Self.stateCaptions.CaptionOff) OR
     (DataSource.DataSet.FieldByName(DataField).IsNull) then STATE:=TSSOFF
                                                        else STATE:=TSSON;
end;

destructor TDBToggleSwitch.Destroy;
begin
   FDataLink.Free;
   FDataLink:=nil;
   inherited Destroy;
end;

function TDBToggleSwitch.GetDataField: string;
begin
   Result:=FDataLink.FieldName;
end;

function TDBToggleSwitch.GetDataSource: TDataSource;
begin
   Result:=FDataLink.DataSource;
end;

procedure TDBToggleSwitch.Notification(AComponent: TComponent; Operation: TOperation);
begin
   inherited Notification(Acomponent,Operation);
   if (Operation=opRemove) AND (FDataLink<>nil) and (AComponent=DataSource) then
       DataSource:=nil;
end;

procedure TDBToggleSwitch.SetDataField(const Value: string);
begin
   FDataLink.FieldName:=Value;
end;

procedure TDBToggleSwitch.SetDataSource(Value: TDataSource);
BEGIN
     if (FDataLink.DataSource<>Value) then
     begin
       FDataLink.DataSource:=Value;
       If Value<>nil then Value.FreeNotification(self);
     end;
end;

end.
__________________
Un saludo desde Canarias, "El abuelo Cebolleta"
Responder Con Cita