尝试将过程分配给动态创建的TShape对象时出错

时间:2018-12-16 17:02:04

标签: delphi

我正在编写一个程序,其中TShape对象类似于棋盘游戏的磁贴。我想稍后在程序中为每个形状使用OnMouseUp事件处理程序。我收到错误-E2009不兼容类型:“参数列表不同”-尝试将TShape的OnMouseDown事件分配给TileClick过程时。 我正在使用TShape的二维数组来创建16 * 25的瓷砖网格。 是否可以将所有TShape对象链接到同一过程?

这是到目前为止的程序:

unit TH_u;

interface

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

type
  TfrmTilesHighlight = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure TileClick(Sender: TObject);
  private
    { Private declarations }
    Tiles : array[1..16,1..25] of TShape;
  public
    { Public declarations }
  end;

var
  frmTilesHighlight: TfrmTilesHighlight;

implementation

{$R *.dfm}

procedure TfrmTilesHighlight.FormCreate(Sender: TObject);
var I, J : Integer;
begin
  for I := 1 to 16 do
    begin
      for J := 1 to 25  do
        begin
          Tiles[I,J] := TShape.Create(Self);
          Tiles[I,J].Parent := frmTilesHighlight;

          //Appearance of Tiles
          Tiles[I,J].Brush.Color := $006CB6FF;
          Tiles[I,J].Pen.Width := 3;
          Tiles[I,J].Pen.Color := clBlack;
          Tiles[I,J].Width := 35;
          Tiles[I,J].Height := 35;
          Tiles[I,J].Shape := stRoundSquare;

          //Formula to determine Top and Left for each tile
          Tiles[I,J].Top := 38*I - 30;
          Tiles[I,J].Left := 38*J - 30;

          //Create OnMouseDown procedure
          Tiles[I,J].OnMouseUp := TileClick; //E2009 Incompatible types: 'Parameter lists differ'
        end;
    end;
end;

procedure TfrmTilesHighlight.TileClick(Sender: TObject);
begin
  //Do whatever should happen when a tile is clicked
end;

end.

1 个答案:

答案 0 :(得分:0)

阅读文档,甚至只是看看通过Object Inspector在设计时分配事件时IDE为您生成的代码。

OnClick事件处理程序的参数与OnMouse(Down|Up)事件处理程序的参数不同。

OnClick仅具有1个参数-Sender

OnMouse(Down|Up)除了Sender-ButtonShiftXY还有4个额外的参数。

因此,您需要将这些额外的参数添加到OnMouseUp事件处理程序中:

procedure TileMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

...

procedure TfrmTilesHighlight.TileMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  // ...
end;

...

Tiles[I,J].OnMouseUp := TileMouseUp;

否则,请使用OnClick事件而不是OnMouseUp事件:

procedure TileClick(Sender: TObject);

...

procedure TfrmTilesHighlight.TileClick(Sender: TObject);
begin
  // ...
end;

...

Tiles[I,J].OnClick := TileClick;