1

我的应用程序有 350 个编辑字段,它们都应该有一个 OnMouseMove 事件。我已经为所有这些生成了这段代码:

...

type

...    
procedure Edit1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure Edit2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);    
...

implementation

{$R *.dfm}

...    
procedure TForm1.Edit1MouseMove(Sender: TObject; Shift: TShiftState; X,  
  Y: Integer);  
begin 
  Edit1.SetFocus(); 
end;

procedure TForm1.Edit2MouseMove(Sender: TObject; Shift: TShiftState; X,  
  Y: Integer);
begin 
  Edit2.SetFocus(); 
end;    
...

但是我没有去对象检查器双击 OnMouseMove。有没有办法在没有对象检查器的情况下完成这项工作。您是否有示例代码行可以使其适用于第一个编辑字段?

4

1 回答 1

9

您可以创建一次并自己在代码中分配它:

type
  TForm1=class(TForm)
    procedure EditMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    //...
  end;

implementation

procedure TForm1.EditMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
  CurrEdit: TEdit;
begin
  if (Sender is TEdit) then
  begin
    CurrEdit := TEdit(Sender);
    // Do whatever with CurrEdit
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.OnMouseMove := EditMouseMove;
  Edit2.OnMouseMove := EditMouseMove;
  Edit3.OnMouseMove := EditMouseMove;
end;

如果您想为TEdit表单上的每个人分配相同的人:

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to ControlCount - 1 do
    if Controls[i] is TEdit then
      TEdit(Controls[i]).OnMouseMove := EditMouseMove;
end;
于 2013-02-17T05:56:23.493 回答