delphi - 如何使用单独的 Label.Caption 更新来操纵某些 Control.OnMouseEnter 事件

标签 delphi delphi-xe2

我有 12 个形状

Shape1
Shape2
.....
Shape12

我有 12 个标签

Label13
Label14
......
Label24

我想知道是否有一种方法可以编写这样一个函数,即在鼠标输入形状时将相应的标签分配给不同的标签,例如Label25:

Label25 :=  
OnMouseEnter
shape1 -> label13
shape2 -> label14
...
shape12 -> label24

因此,如果鼠标进入Shape1,Label25将等于Label13,如果鼠标进入Shape2,Label25将等于Label14,并继续,直到鼠标进入Shape12,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.

但每次运行时我都会失败。 我发送的信息有误吗?

最佳答案

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

var
  ShapeOrderNo: Integer;
  FoundComponent: TComponent;
begin
  // first try to convert a text behind "Shape", what should be a shape's order 
  // and if it's convertable to integer, then...
  if TryStrToInt(Copy(TShape(Sender).Name, 6, MaxInt), ShapeOrderNo) then
  begin
    // try to find a component with the name "label" + found shape order number
    // incremented by 12
    FoundComponent := FindComponent('label' + IntToStr(ShapeOrderNo + 12));
    // if the component is found, or to be more specific, if it's TLabel, then...
    if (FoundComponent is TLabel) then
      TLabel(FoundComponent).Caption := 'Hello from ' + TShape(Sender).Name;
  end;
end;

关于delphi - 如何使用单独的 Label.Caption 更新来操纵某些 Control.OnMouseEnter 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12969030/

相关文章:

delphi - 测试未在接口(interface)部分声明的类

delphi - VirtualTreeView:编辑下一列

delphi - 如何从Windows获取用于存储缓存缩略图的永久目录?

PHP 函数到 Delphi 函数

delphi - 如何调试使用 regsvr32.exe 制作的 64 位 dll 注册过程?

json - 如何将 JSON 字符串转换为图像?

delphi - 如何从服务应用程序使用 FindWindow()?

Delphi非可视化组件图像

Delphi绘制一个有两个圆角和直角的封闭矩形

Delphi AdoConnection 重新连接