Ver Mensaje Individual
  #1  
Antiguo 03-02-2011
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
Mi nuevo Componente NewPanelDB

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 [-]
//****************************************************************************************************  **********
//  NewPanelDb                                           03/02/2011
//
// Panel en el que no tenemos que controlar si esta enble o visible ya que lo detecta del Estado del datasourse
// puede controlar en estado norma (cuando esta editando o insertando) esta en enable o visible, por ejemplo,
// panel de los datos panel con los botones grabar y cancelar, etc.. O en inverso (cuando esta editando o
//  insertando) no esta enable o visible, por ejemplo panel con la botonera para movernos por los registros,
//  busqueda, nuevo, modificar, borrar, salir, imprimir, etc...
//--------------------------------------------------------------------------------------------------------------
// Aunque la idea Original es mía J.L.G.T. debo agradecer muchisimo la ayuda de Andres1569 de DelphiAccess sin
// el cual no estaría terminado este componente
//
// Como todos mis componentes, puedes usarlo gratuitamente sin restricciones de ningún tipo
//--------------------------------------------------------------------------------------------------------------
//
// Propiedades
//
//  Action: TTypeAction;           //Tipo de Acción a usar panel enabled o Visible
//  InverseAction: Boolean;    //Si queremos que trabaje al revés, lo normal es State  es DsEdit Panel seria
//                  Enabled, si esta opción esta seleccionada seria no enabled
//  UseColor: Boolean;           //Usamos Color En Edit y Color En no Edit
//  ActiveColor: Tcolor;           //Color a usar cuando este en edit o insert
//  ColorNotActive:Tcolor;         //Color a usar cuando no
//
//****************************************************************************************************  ***********



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;                     //Tipo de Acción a usar panel enabled o Visible
    FInverseAction: Boolean;                  // Si queremos que trabaje al reves, lo normal es
                                              //State  es DsEdit Panelñ seria Enabled, si esta
                                              //opcion esta seleccionada seria  no enabled
    FUseColor: Boolean;                       //USamos Color En Edit y Color En no Edit
    FActiveColor: Tcolor;                     //Color a usar cuando este en edit o insert
    FColorNotActive:Tcolor;                   //Color a usar cuando no
    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

{ TPanelDataLink }

procedure TPanelDataLink.EditingChanged;
begin
  if Assigned(FOnEditingChange) then FOnEditingChange(Self);
end;

{ TMiPanel }

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;                     //Esta y la segunda linea es para asegurar los cambios posteriores
  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     //Caundo insertamos o editamos
       if FAction=xEnable then              //Usando Enabled
       begin
          if FInverseAction=False then  Self.Enabled:=True
                                  else  Self.Enabled:=False;
       end else
       begin                                //Usando Visible
          if FInverseAction=false then  Self.Visible:=True
                                  else  Self.Visible:=False;
       end;
       if FUseColor=true then  self.Color:=FActiveColor;
    end else
    begin   //Caundo no insertamos o editamos
       if FAction=xEnable then              //Usando Enabled
       begin
          if FInverseAction=False then  Self.Enabled:=False
                                  else  Self.Enabled:=True;
       end else
       begin                                //Usando Visible
          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.
__________________
Un saludo desde Canarias, "El abuelo Cebolleta"
Responder Con Cita