Page 1 of 1

Problem with Creating component (from TMyQuery)

Posted: Wed 10 Sep 2008 12:37
by saidus
Hello Freinds !!!
I want to develope a component inherited from TMyQuery but i receive an Error messages at design time even at run time so, here is the code

Code: Select all

unit bsClientQuery;

interface

uses
  SysUtils, Classes, Windows, Dialogs, bsBase, DB, DBAccess,Controls, DBCtrls,cxDBEdit,
  Graphics, Forms, TaskDialog, TaskDialogEx  ;

type
  TBSClientQuery = class ;
  TBSClientOption = class (TPersistent)
  private
    FClient : TBSClientQuery;
    FEditor: TcxDBTextEdit;
    FRequiredField_1: TStringField;
    FRequiredField_2: TStringField;
    FAskbeforeDelete: Boolean;
    FDeleteMsg: string;
    procedure SetEditor(const Value: TcxDBTextEdit);
    procedure SetRequiredField_1(const Value: TStringField);
    procedure SetRequiredField_2(const Value: TStringField);

    procedure CtrlDelete;
    procedure CtrlEditor;
    procedure CtrlEmptyField;

  protected
  public
    constructor Create(Client : TBSClientQuery);
    destructor Destroy ; override;
  published
    property RequiredField_1: TStringField read FRequiredField_1 write SetRequiredField_1;
    property RequiredField_2: TStringField read FRequiredField_2 write SetRequiredField_2;
    property Editor: TcxDBTextEdit read FEditor write SetEditor ;
    property DeleteMsg: string read FDeleteMsg write FDeleteMsg;
    property AskbeforeDelete: Boolean read FAskbeforeDelete write FAskbeforeDelete;

  end;
{ ==================================================================================== }
{  Exception Handler will intercept all exceptions in levels :                         }
{  ------------------------                                                            }
{   +   Before insert
    +   Before Post
    +   Before Edit
    +   Before Delete
    +   Before Cancel
    +   Before Refresh
}
  TBSClientQuery = class(TBSDataSet)
  private
    FAfterCancel: TDataSetNotifyEvent;
    FAfterDelete: TDataSetNotifyEvent;
    FAfterEdit: TDataSetNotifyEvent;
    FAfterInsert: TDataSetNotifyEvent;
    FAfterPost: TDataSetNotifyEvent;
    FBeforeCancel: TDataSetNotifyEvent;
    FBeforeDelete: TDataSetNotifyEvent;
    FBeforeEdit: TDataSetNotifyEvent;
    FBeforeInsert: TDataSetNotifyEvent;
    FBeforePost: TDataSetNotifyEvent;
    F_Client: TBSClientOption;
    OldException : TExceptionEvent;
  protected
    procedure DoAfterCancel; override;
    procedure DoAfterDelete; override;
    procedure DoAfterEdit; override;
    procedure DoAfterInsert; override;
    procedure DoAfterPost; override;
    procedure DoBeforeCancel; override;
    procedure DoBeforeDelete; override;
    procedure DoBeforeEdit; override;
    procedure DoBeforeInsert; override;
    procedure DoBeforePost; override;
    procedure BSFireException(Sender:TObject;E:Exception);
    procedure BSInstallExecption;
    procedure BSUninstallException;

    procedure Notification(AComponent: TComponent; Operation: TOperation); override;

  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property BeforeInsert: TDataSetNotifyEvent read FBeforeInsert write FBeforeInsert;
    property AfterInsert: TDataSetNotifyEvent read FAfterInsert write FAfterInsert;
    property BeforeEdit: TDataSetNotifyEvent read FBeforeEdit write FBeforeEdit;
    property AfterEdit: TDataSetNotifyEvent read FAfterEdit write FAfterEdit;
    property BeforePost: TDataSetNotifyEvent read FBeforePost write FBeforePost;
    property AfterPost: TDataSetNotifyEvent read FAfterPost write FAfterPost;
    property BeforeCancel: TDataSetNotifyEvent read FBeforeCancel write FBeforeCancel;
    property AfterCancel: TDataSetNotifyEvent read FAfterCancel write FAfterCancel;
    property BeforeDelete: TDataSetNotifyEvent read FBeforeDelete write FBeforeDelete;
    property AfterDelete: TDataSetNotifyEvent read FAfterDelete write FAfterDelete;

    property _Client: TBSClientOption read F_Client write F_Client;
  end;



implementation

//uses cxContainer ;

{$I bsMessage.inc}
{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }

procedure TBSClientQuery.BSFireException(Sender: TObject; E: Exception);
var
  ErCode: Integer;
  Mesge : string;
begin
  { illustate all excetion messages }
  { 1. DataSet Closed ... Er Code = 18-26-70-22 ... }
  if Enil then
  begin
     ErCode := EDAError(E).ErrorCode;
     Mesge  := EDAError(E).Message + ' : ' + IntToStr(ErCode);

     case ErCode of
       1062 : begin
                    bsPrintMessage(g_Caption ,'Duplicate Key ',cl_exist    ,
                                   g_Email+g_tel,tiError,[cbOk],g_msys,Mesge,idOk);
                    Abort ;
              end;
       1451 : begin
                    bsPrintMessage(g_Caption ,'Delete Error  ',cl_deleteErr,
                                   g_Email+g_tel,tiError,[cbOk],g_msys,Mesge,idOk);
                    Abort ;
              end;
       else   begin
                    bsPrintMessage(g_Caption ,'Erreur Systeme',g_Error    ,
                                   g_Email+g_tel,tiError,[cbOk],g_msys,Mesge,idOk);
                    Abort ;
              end;
     end;
  end;

end;

{ -------------------------------------------------------}
{}
{ -------------------------------------------------------}
procedure TBSClientQuery.BSInstallExecption;
begin
{    OldException := Application.OnException ;
    Application.OnException := BSFireException;   }
end;

{ -------------------------------------------------------}
{}
{ -------------------------------------------------------}
procedure TBSClientQuery.BSUninstallException;
begin
  //  Application.OnException := OldException;
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
constructor TBSClientQuery.Create(AOwner: TComponent);
begin
  inherited;
  F_Client := TBSClientOption.Create(Self);
//  ShowMessage('Cration de TPersistent Fait ');
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
destructor TBSClientQuery.Destroy;
begin
  //FreeAndNil(F_Client);
  F_Client.Free;
  inherited;
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientQuery.DoAfterCancel;
begin
{  F_Client.CtrlEditor;
  BSUninstallException;
  if Assigned(FAfterCancel) then FAfterCancel(self);  }
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientQuery.DoAfterDelete;
begin
{  F_Client.CtrlEditor;
  BSUninstallException;
  if Assigned(FAfterDelete) then FAfterDelete(self);    }
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientQuery.DoAfterEdit;
begin
{  BSUninstallException;
  if Assigned(FAfterEdit) then FAfterEdit(Self);}
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientQuery.DoAfterInsert;
begin
  // Allow the Edit Field of Code to be not ReadOnly
{   F_Client.CtrlEditor;
   BSUninstallException;
  if Assigned(FAfterInsert) then FAfterInsert(Self);}
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientQuery.DoAfterPost;
begin
{  F_Client.CtrlEditor;
  BSUninstallException;
  if Assigned(FAfterPost) then FAfterPost(Self);}
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientQuery.DoBeforeCancel;
begin
{  BSInstallExecption;
  if Assigned(FBeforeCancel) then FBeforeCancel(self);}
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientQuery.DoBeforeDelete;
begin
  //  Asking before delete ...
{  _Client.CtrlDelete;
  BSInstallExecption;
  if Assigned(FBeforeDelete) then FBeforeDelete(Self);}
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientQuery.DoBeforeEdit;
begin
{  F_Client.CtrlEditor;
  BSInstallExecption;
  if Assigned(FBeforeEdit) then FBeforeEdit(Self);     }
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientQuery.DoBeforeInsert;
begin
{  BSInstallExecption;
  if Assigned(FBeforeInsert) then FBeforeInsert(Self);}
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientQuery.DoBeforePost;
begin
  //  If fields empty then ASK and abort
{  _Client.CtrlEmptyField;
  BSInstallExecption;
  if Assigned(FBeforePost) then FBeforePost(Self);}
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientQuery.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent,Operation);
  if (Operation = opRemove) and (AComponent=F_Client.FEditor) then
      F_Client.FEditor := nil;
  if (Operation = opRemove) and (AComponent=F_Client.FRequiredField_1) then
       F_Client.FRequiredField_1 := nil;
  if (Operation = opRemove) and (AComponent=F_Client.FRequiredField_2) then
       F_Client.FRequiredField_2 := nil; 
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
constructor TBSClientOption.Create(Client: TBSClientQuery);
begin
  FClient := Client;
//  ShowMessage('Ammorcage fait - FClient in _Client');
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientOption.CtrlDelete;
begin
  if FAskbeforeDelete then
  begin
    if bsPrintMessage(g_Caption ,'Suppression - _Client ?',
                      {Format(cl_askDelete,[FRequiredField_2.Value])}cl_askDelete,
                      g_Email+g_tel,tiQuestion,[cbYes,cbNo],'','',idNo) = mrNo
    then
       Abort;
  end;
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientOption.CtrlEditor;
begin
  with FEditor do
  begin
       if FClient.State=dsInsert then
          begin
            Properties.ReadOnly := false;
            Style.Color := clWindow;
          end
       else
          begin
            Properties.ReadOnly := True;
            Style.Color := $00E2EFF1;
          end;
  end;    // with
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientOption.CtrlEmptyField;
begin
{  if (CompareStr(FRequiredField_1.Value,NullStr^)=0) or
     (CompareStr(FRequiredField_2.Value,NullStr^)=0) then
  begin
    bsPrintMessage(g_Caption ,g_CodRs,cl_emptyFields,g_Email+g_tel,tiError,[cbOk],'','', idOk);
    Abort;
  end;  }
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
destructor TBSClientOption.Destroy;
begin
  FClient := nil;
  inherited;
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientOption.SetEditor(const Value: TcxDBTextEdit);
begin
//  FEditor := Value;
//  CtrlEditor;
//{
//if FEditornil then
//     FEditor.FreeNotification(FClient);
  if Assigned(FEditor) and (not (csDestroying in FEditor.ComponentState)) then
     FEditor.RemoveFreeNotification(FClient);
  FEditor := Value;
  if Assigned(FEditor) then
     FEditor.FreeNotification(FClient);
  CtrlEditor;
//  ShowMessage('Editor was Set ...');
//     }
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- } 
procedure TBSClientOption.SetRequiredField_1(const Value: TStringField);
begin
//  FRequiredField_1 := Value;
  if Assigned(FRequiredField_1) and (not (csDestroying in FRequiredField_1.ComponentState)) then
      FRequiredField_1.RemoveFreeNotification(FClient);
  FRequiredField_1 := Value;
  if Assigned(FRequiredField_1) then
     FRequiredField_1.FreeNotification(FClient);
end;

{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }
procedure TBSClientOption.SetRequiredField_2(const Value: TStringField);
begin
//  FRequiredField_2 := Value;
  if Assigned(FRequiredField_2) and (not (csDestroying in FRequiredField_2.ComponentState)) then
      FRequiredField_2.RemoveFreeNotification(FClient);
  FRequiredField_2 := Value;
  if Assigned(FRequiredField_2) then
     FRequiredField_2.FreeNotification(FClient);
end;
{ ------------------------------------------------------- }
{}
{ ------------------------------------------------------- }

end.
It seems that the code is all right cause when i change the Class from with i inherite the query (TMyQuery to some class else) all works well
by the way when the error occures (i use the ErekaLog components ) it points derectelly to the notification methode then the destructor.
Is this a bug from MyDAc ??
Please Help me !!
:(

Posted: Thu 11 Sep 2008 07:51
by Dimon
I can not reproduce the problem.
Please send at dmitryg*devart*com a complete sample, that contains the code of the inherited class from TMyQuery and code, that calls this class and arises an error.

Posted: Thu 11 Sep 2008 13:51
by saidus
Proviet Dima !!!
Check your Email Box

Posted: Thu 11 Sep 2008 15:51
by Dimon
Thank you for information. We have answered you by e-mail.

Posted: Sun 14 Sep 2008 08:43
by saidus
Spocibo Dima!!
It Works Well - Spocibo iche !!
Vse ravno ne poemu kak eto rabotaet !