如何为TcxGrid共享OnEnter事件?

时间:2010-06-02 06:46:57

标签: delphi events delphi-2007

我想在Devexpress TcxGrid上标准化行为。所以我有一个带有事件的单元ClientData:

procedure TClientData.grdOnEnter(Sender: TObject);
begin
  mnuCxGridStandardMenu.Grid := TcxGrid(Sender);
end;

如果我尝试在另一个单元中的TcxGrid.OnEnter事件中使用该事件,则拒绝工作。 如果我粘贴在ClientData.grdOnEnter中,则会出现一个对话框,表明它不是有效的标识符。

是否可以在Delphi 2007中分享几个单元的活动?

编辑: ClientData是一个在启动时创建并在应用程序退出时释放的数据模块。引用位于全局var ClientData中。所以我认为应该从其他单位引用它。

2 个答案:

答案 0 :(得分:1)

您可以这样做,只要您的表单单元在其uses子句中具有ClientData单元并且存在且可以访问正确实例化的TClientData对象。 (不要混淆类定义和该类类型的对象)。然后,例如,您可以向OnCreate处理程序添加类似的内容:

// Optional - ClientData may be a 'global' object so won't need creating.
// FClientData could be a form member ora more globally accessible variable.
//FClientData := TClientData.Create(Self); 
// or
//FClientData := TClientData.Create(Application);
cxGrid1.OnEnter := FClientData.grdOnEnter;

如果[F] ClientData对象在表单之前被销毁,那么当某些东西试图进入OnEnter方法时,您将获得访问冲突。

答案 1 :(得分:1)

是的,如果你通过代码分配事件处理程序,它应该有用 如果您的事件处理程序不使用ClientData实例中的任何内容(推荐),您甚至不需要创建实例 TClientData类型的nil变量就足够了。

在下面的示例应用程序中,dpr不会自动创建ClientData模块,并且仍为零。 这不会阻止事件处理程序正常工作。

dpr

program Project2;

uses
  Forms,
  Unit10 in 'Unit10.pas' {Form10},
  Unit11 in 'Unit11.pas' {ClientData: TDataModule};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm10, Form10);
  Application.Run;
end.

表格dfm

object Form10: TForm10
  Left = 0
  Top = 0
  Caption = 'Form10'
  ClientHeight = 282
  ClientWidth = 418
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 168
    Top = 168
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
end

Form pas

unit Unit10;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Unit11, StdCtrls;

type
  TForm10 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form10: TForm10;

implementation

{$R *.dfm}

procedure TForm10.Button1Click(Sender: TObject);
begin
  if ClientData = nil then
    ShowMessage('ClientData is nil')
  else
    ShowMessage('ClientData is NOT nil');
end;

procedure TForm10.FormCreate(Sender: TObject);
begin
  OnClick := ClientData.WhateverEvent;
end;

end.

DataModule dfm

object ClientData: TClientData
  OldCreateOrder = False
  Height = 150
  Width = 215
end

DataModule pas

unit Unit11;

interface

uses
  SysUtils, Classes, Windows;

type
  TClientData = class(TDataModule)
    procedure WhateverEvent(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  ClientData: TClientData;

implementation

{$R *.dfm}

procedure TClientData.WhateverEvent(Sender: TObject);
begin
  MessageBox(0, PChar('Sender is ' + Sender.ClassName), 'Test', MB_ICONINFORMATION or MB_OK);
end;

end.