Delphi 如何使用单独的Label.Caption更新来装配一些Control.onmouseinter事件

Delphi 如何使用单独的Label.Caption更新来装配一些Control.onmouseinter事件,delphi,delphi-xe2,Delphi,Delphi Xe2,我有12种形状 Shape1 Shape2 ..... Shape12 我有12个标签 Label13 Label14 ...... Label24 我想知道是否有一种方法可以编写这样一个函数,在鼠标上输入形状会将相应的标签指定给不同的标签,例如Label25: Label25 := OnMouseEnter shape1 -> label13 shape2 -> label14 ... shape12 -> label24 因此,如果鼠标进入形状1,Label25将

我有12种形状

Shape1
Shape2
.....
Shape12
我有12个标签

Label13
Label14
......
Label24
我想知道是否有一种方法可以编写这样一个函数,在鼠标上输入形状会将相应的标签指定给不同的标签,例如Label25:

Label25 :=  
OnMouseEnter
shape1 -> label13
shape2 -> label14
...
shape12 -> label24
因此,如果鼠标进入形状1,Label25将与Label13相等,如果鼠标进入形状2,Label25将与Label14相等,并继续,直到鼠标进入形状12,Label25将与Label24相等

我知道我会写作

label25 := labelxx 
在每个鼠标上输入事件。但是当标签的名称和形状相对应时,会有一种更简单的方法,每次标签比形状多12个

在添加建议后,我添加了这个

procedure TFZone1Mod7.ChangeText(sender: TObject);
var
  ShapeOrderNo: integer;
  FoundComponent: TComponent;
begin
  if TryStrToInt(copy(TShape(Sender).Name,6,MaxInt),ShapeOrderNo) then
    begin
      FoundComponent := FindComponent('label'+inttostr(ShapeOrderNo+12));
      if (FoundComponent is TLabel) then
            Label25.Caption := TLabel(FoundComponent).Caption
      else
          showmessage('not found');
    end;
  showmessage('failed try');

end;

procedure TFZone1Mod7.Shape1MouseEnter(Sender: TObject);
begin
    changetext(self);
end;

end.
但每次它运行时,我都会失败。
我是否发送了错误的信息?

我不喜欢这种设计,但是,您可以对所有形状使用公共事件处理程序,并使用函数按名称查找组件。然后您可以这样编写Cometing(请注意,它未经测试,仅在浏览器中编写):


我不喜欢这种设计,但是,您可以对所有形状使用公共事件处理程序,并使用函数按名称查找组件。然后您可以这样编写Cometing(请注意,它未经测试,仅在浏览器中编写):


在TShape上添加属性

 TMyShape= class(TShape)
 private
   FLinkLabel: TLabel;
 procedure SetLinkLabel(const Value: TLabel);
 published
   property LinkLabel: TLabel read FLinkLabel write SetLinkLabel;
 end;

 procedure TMyShape.SetLinkLabel(const Value: TLabel);
 begin
   FLinkLabel := Value;
 end;

 procedure TForm1.FormCreate(Sender: TObject);
 var
   oMyShape: TMyShape;
 begin
   oMyShape:= TMyShape.Create(self);
   oMyShape.Parent:= Self;
   oMyShape.LinkLabel:= self.Label1;
   oMyShape.OnMouseEnter:= OnShapeMouseEnter;
 end;

 procedure TForm1.OnShapeMouseEnter(Sender: TObject);
 begin
   if (sender is TMyShape) and
      ( TMyShape(Sender).LinkLabel <>Nil) then
   begin
     TMyShape(Sender).LinkLabel.Caption:= 'Hello';
   end;
 end;
TMyShape=class(TShape)
私有的
弗林克莱贝尔:特拉贝尔;
程序SetLinkLabel(常量值:TLabel);
出版
属性LinkLabel:TLabel read FLinkLabel write SetLinkLabel;
结束;
程序TMyShape.SetLinkLabel(常量值:TLabel);
开始
FLinkLabel:=值;
结束;
过程TForm1.FormCreate(发送方:TObject);
变量
肌状:TMyShape;
开始
myShape:=TMyShape.Create(self);
myshape.Parent:=Self;
myShape.LinkLabel:=self.Label1;
onMouseCenter:=onShapeMouseCenter;
结束;
程序TForm1.OnShapeMouseEnter(发送方:ToObject);
开始
如果(发送方为TMyShape)和
(TMyShape(Sender).LinkLabel Nil)然后
开始
TMyShape(Sender.LinkLabel.Caption:=“你好”;
结束;
结束;

我只是在表单上设置了一个tlabel,并将标签与TMyShape链接。

在TShape上添加一个属性

 TMyShape= class(TShape)
 private
   FLinkLabel: TLabel;
 procedure SetLinkLabel(const Value: TLabel);
 published
   property LinkLabel: TLabel read FLinkLabel write SetLinkLabel;
 end;

 procedure TMyShape.SetLinkLabel(const Value: TLabel);
 begin
   FLinkLabel := Value;
 end;

 procedure TForm1.FormCreate(Sender: TObject);
 var
   oMyShape: TMyShape;
 begin
   oMyShape:= TMyShape.Create(self);
   oMyShape.Parent:= Self;
   oMyShape.LinkLabel:= self.Label1;
   oMyShape.OnMouseEnter:= OnShapeMouseEnter;
 end;

 procedure TForm1.OnShapeMouseEnter(Sender: TObject);
 begin
   if (sender is TMyShape) and
      ( TMyShape(Sender).LinkLabel <>Nil) then
   begin
     TMyShape(Sender).LinkLabel.Caption:= 'Hello';
   end;
 end;
TMyShape=class(TShape)
私有的
弗林克莱贝尔:特拉贝尔;
程序SetLinkLabel(常量值:TLabel);
出版
属性LinkLabel:TLabel read FLinkLabel write SetLinkLabel;
结束;
程序TMyShape.SetLinkLabel(常量值:TLabel);
开始
FLinkLabel:=值;
结束;
过程TForm1.FormCreate(发送方:TObject);
变量
肌状:TMyShape;
开始
myShape:=TMyShape.Create(self);
myshape.Parent:=Self;
myShape.LinkLabel:=self.Label1;
onMouseCenter:=onShapeMouseCenter;
结束;
程序TForm1.OnShapeMouseEnter(发送方:ToObject);
开始
如果(发送方为TMyShape)和
(TMyShape(Sender).LinkLabel Nil)然后
开始
TMyShape(Sender.LinkLabel.Caption:=“你好”;
结束;
结束;

我只是在表单上设置了一个tlabel,并将标签与TMyShape链接。

如果您不在Forms Designer中进行控件管理并在运行时创建它们,我会这样做:

procedure TForm1.OnShapeMouseEnter(Sender: TObject);
begin
  Assert(Sender is TShape);
  Label25.Caption := fLabels[TShape(Sender).Tag].Caption;
end;
声明标签数组和形状数组:

const
  ShapesCount = 20;

type
  TForm1 = class(TForm)
    fLabels: array [0..ShapesCount-1] of TLabel;
    fShapes: array [0..ShapesCount-1] of TShape;
在运行时,为每个形状指定一个
标记及其索引

procedure TForm1.OnFormCreate;
begin
  for I := 0 to ShapesCount - 1 do  
  begin
    fShapes[I] := TShape.Create(..);
    fLabels[I] := TLabel.Create(..);
    fShapes[I].Tag := I;
    fShapes[I].OnMouseEnter := OnShapeMouseEnter;
  end;
end;
然后你可以这样使用它:

procedure TForm1.OnShapeMouseEnter(Sender: TObject);
begin
  Assert(Sender is TShape);
  Label25.Caption := fLabels[TShape(Sender).Tag].Caption;
end;


编辑:再看一点,您可能已经有了一个带有Lable.Caption的数组,因此您可以从那里直接获取Label25.Caption。

如果您不在Forms Designer中进行控件管理并在运行时创建它们,我会这样做:

procedure TForm1.OnShapeMouseEnter(Sender: TObject);
begin
  Assert(Sender is TShape);
  Label25.Caption := fLabels[TShape(Sender).Tag].Caption;
end;
声明标签数组和形状数组:

const
  ShapesCount = 20;

type
  TForm1 = class(TForm)
    fLabels: array [0..ShapesCount-1] of TLabel;
    fShapes: array [0..ShapesCount-1] of TShape;
在运行时,为每个形状指定一个
标记及其索引

procedure TForm1.OnFormCreate;
begin
  for I := 0 to ShapesCount - 1 do  
  begin
    fShapes[I] := TShape.Create(..);
    fLabels[I] := TLabel.Create(..);
    fShapes[I].Tag := I;
    fShapes[I].OnMouseEnter := OnShapeMouseEnter;
  end;
end;
然后你可以这样使用它:

procedure TForm1.OnShapeMouseEnter(Sender: TObject);
begin
  Assert(Sender is TShape);
  Label25.Caption := fLabels[TShape(Sender).Tag].Caption;
end;


编辑:再看一点,您可能已经有了一个带有Lable.Caption的数组,因此您可以从那里获取Label25.Caption。

如果不想重新编写标签的创建,请使用findcomponent:

所有形状的处理程序都相同

procedure TForm1.Shape1MouseEnter(Sender: TObject);
var
  ActiveShape: TShape;
  StrName: string;
  ActiveLabel: TComponent;
begin
  if Sender is TShape then
  begin
    ActiveShape := TShape(Sender);
// Only works if Shapes are named Shape1..12 and the labels Label1..12 !!
    StrName := 'Label' + copy(ActiveShape.name, 6, length(ActiveShape.name)); 
    ActiveLabel := FindComponent(StrName);
    if ActiveLabel is TLabel then
      Label25.Caption := TLabel(ActiveLabel).Caption;
  end;
end;
如果有点混乱,这将满足您的需要:)

[编辑] 我突然想到,有一种简单的方法可以改进这一点,并满足您对使用不同“名称\编号”标签的需求:

在设计器中,将每个TShape的Tag属性设置为要与其关联的标签号。因此,将某人的标签形状设置为13(以保留问题)


如果不想重新写入标签的创建,请使用findcomponent:

所有形状的处理程序都相同

procedure TForm1.Shape1MouseEnter(Sender: TObject);
var
  ActiveShape: TShape;
  StrName: string;
  ActiveLabel: TComponent;
begin
  if Sender is TShape then
  begin
    ActiveShape := TShape(Sender);
// Only works if Shapes are named Shape1..12 and the labels Label1..12 !!
    StrName := 'Label' + copy(ActiveShape.name, 6, length(ActiveShape.name)); 
    ActiveLabel := FindComponent(StrName);
    if ActiveLabel is TLabel then
      Label25.Caption := TLabel(ActiveLabel).Caption;
  end;
end;
如果有点混乱,这将满足您的需要:)

[编辑] 我突然想到,有一种简单的方法可以改进这一点,并满足您对使用不同“名称\编号”标签的需求:

在设计器中,将每个TShape的Tag属性设置为要与其关联的标签号。因此,将某人的标签形状设置为13(以保留问题)


如果我这样做,我仍然需要添加Label25.caption:=flabels[tshape(sender.tag)];对于每个OnMouseCenter()?否,为每个shapes OnMouseCenter事件指定相同的处理程序。处理程序检查发送者形状具有什么标记,并采用适当的标签标题。这就是重点;)我确实认为它会起作用,但我不会在运行时创建。我当然应该在问题上加上这一点。但仍将给予信任。然后,您需要一个数组,该数组表示哪个形状与哪个标签关联,并使其适应TLama答案。如果我这样做,我仍然必须添加Label25.caption:=flabels[tshape(sender).tag]。caption;对于每个OnMouseCenter()?否,为每个shapes OnMouseCenter事件指定相同的处理程序。处理程序检查发送者形状具有什么标记,并采用适当的标签标题。这就是重点;)我确实认为它会起作用,但我不会在运行时创建。我当然应该在问题上加上这一点。但仍将给予信任。然后您将需要一个数组,该数组表示哪个形状与哪个标签和标签相关联