Es un panel para usar con bases de datos, de manera que no tengamos que controlar cundo una serie de controles esta enables o visibles ya que al estar dentro de este panel, nos permite poder olvidarnos de tener que estar controlando estas situaciones.
os pongo el código completo, ya que no es muy grande.
Código Delphi
[-]
unit NewPanelDB;
interface
uses
SysUtils, Classes, Controls, ExtCtrls, Db, Graphics, Dialogs;
type
TTypeAction= (xEnable,xVisible);
TPanelDataLink = class(TDataLink)
private
FOnEditingChange: TNotifyEvent;
protected
procedure EditingChanged; override;
public
property OnEditingChange: TNotifyEvent read FOnEditingChange write FOnEditingChange;
end;
TNewPanelDB = class(TPanel)
private
FDataLink : TPanelDataLink;
FAction: TTypeAction; FInverseAction: Boolean; FUseColor: Boolean; FActiveColor: Tcolor; FColorNotActive:Tcolor; function GetDataSource: TDataSource;
procedure SetDataSource(const Value: TDataSource);
procedure SetAction(const Value: TTypeAction);
procedure SetInverseAction(const Value: Boolean);
procedure SetUseColor(const Value: Boolean);
procedure SetActiveColor(const Value:Tcolor);
procedure SetColorNotActive(Const Value:Tcolor);
protected
procedure EditingChanged(Sender: TObject); virtual;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property DataSource: TDataSource read GetDataSource write SetDataSource;
property Action: TTypeAction read FAction write SetAction default xEnable;
property InverseAction: Boolean read FInverseAction write SetInverseAction default False;
property UseColor: Boolean read FUseColor write SetUseColor default False;
property ActiveColor: Tcolor read FActiveColor write SetActiveColor default clMoneyGreen;
property ColorNotActive:Tcolor read FColorNotActive write SetColorNotActive default clBtnFace;
end;
procedure Register;
implementation
procedure TPanelDataLink.EditingChanged;
begin
if Assigned(FOnEditingChange) then FOnEditingChange(Self);
end;
constructor TNewPanelDB.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDataLink := TPanelDataLink.Create;
FDataLink.OnEditingChange := EditingChanged;
FAction:=xEnable;
FInverseAction:=False;
FUseColor:=False;
FActiveColor:=clMoneyGreen;
FColorNotActive:=clBtnFace;
Color:=clGray; Color:=clBtnFace;
Enabled:=False;
end;
procedure TNewPanelDB.EditingChanged(Sender: TObject);
begin
if Assigned(FDataLink.DataSet) then
if FDataLink.DataSet.State in [dsEdit,dsInsert] then
begin if FAction=xEnable then begin
if FInverseAction=False then Self.Enabled:=True
else Self.Enabled:=False;
end else
begin if FInverseAction=false then Self.Visible:=True
else Self.Visible:=False;
end;
if FUseColor=true then self.Color:=FActiveColor;
end else
begin if FAction=xEnable then begin
if FInverseAction=False then Self.Enabled:=False
else Self.Enabled:=True;
end else
begin if FInverseAction=false then Self.Visible:=False
else Self.Visible:=True;
end;
if FUseColor=true then self.Color:=FColorNotActive;
end;
end;
destructor TNewPanelDB.Destroy;
begin
FDataLink.Free;
inherited Destroy;
end;
function TNewPanelDB.GetDataSource: TDataSource;
begin
result := FDataLink.DataSource;
end;
procedure TNewPanelDB.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) AND (FDataLink <> nil) AND
(AComponent = DataSource) then DataSource := nil;
end;
procedure TNewPanelDB.SetDataSource(const Value: TDataSource);
begin
if NOT (FDataLink.DataSourceFixed AND (csLoading in ComponentState)) then
FDataLink.DataSource := Value;
if Assigned(Value) then Value.FreeNotification(Self);
end;
procedure TNewPanelDB.SetAction(const Value: TTypeAction);
begin
if FAction<>Value then FAction:=Value;
if FAction=xEnable then
begin
if FInverseAction=false then Enabled:=false
else Enabled:=True;
Visible:=True;
end else
begin
if FInverseAction=false then Visible:=false
else Visible:=True;
Enabled:=true;
end;
end;
procedure TNewPanelDB.SetInverseAction(const Value: Boolean);
begin
if FInverseAction<>value then FInverseAction:=Value;
if FAction=xEnable then
begin
if FInverseAction=false then Enabled:=false
else Enabled:=True;
Visible:=true;
end else
begin
if FInverseAction=false then Visible:=false
else Visible:=True;
Enabled:=true;
end;
end;
procedure TNewPanelDB.SetUseColor(const Value: Boolean);
begin
if FUseColor<>value then FUseColor:=Value;
color:=FColorNotActive;
end;
procedure TNewPanelDB.SetActiveColor(const Value: TColor);
begin
if FActiveColor<>value then FActiveColor:=Value;
end;
procedure TNewPanelDB.SetColorNotActive(const Value: TColor);
begin
if FColorNotActive<>value then FColorNotActive:=Value;
color:=FColorNotActive;
end;
procedure Register;
begin
RegisterComponents('Standard', [TNewPanelDB]);
end;
end.