简体   繁体   English

在运行时创建的对象上的双击事件-Delphi

[英]Double click event on object created at run time - Delphi

I have created a simple Delphi form with a button that, when pressed, creates a label object in run time. 我用按钮创建了一个简单的Delphi表单,按下该按钮可在运行时创建一个标签对象。 I have created an on double click event for the label that shows a message to the screen. 我已经为标签创建了一个双击事件,该事件向屏幕显示了一条消息。 The problem is that after creating the label, I have to double click on the form before the double click event works on the label. 问题是创建标签后,我必须双击表单,然后双击事件才能在标签上起作用。 Obviously this is not ideal as I would like to be able to double click on the label and trigger the event without having to first double click the form. 显然,这并不理想,因为我希望能够双击标签并触发事件,而不必先双击表单。

Here is the code for my form: 这是我的表单的代码:

unit Unit4;

interface

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

type
  TForm4 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormDblClick(Sender: TObject);
    procedure MyLabelDblClick(Sender:TObject);
  private
    { Private declarations }
    LabelObject: TLabel;
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.Button1Click(Sender: TObject);
begin
  LabelObject := TLabel.Create(Self);
  LabelObject.Left := 100;
  LabelObject.Top := 100;
  LabelObject.Width := 200;
  LabelObject.Height := 20;
  LabelObject.Visible := True;
  LabelObject.Parent := Self;
  LabelObject.Caption := 'My Run Time Label';
  LabelObject.Cursor := crHandPoint;
end;

procedure TForm4.FormDblClick(Sender: TObject);
begin
  LabelObject.OnDblClick := MyLabelDblClick;
end;

procedure TForm4.MyLabelDblClick(Sender: TObject);
begin
  showmessage('You double clicked My Run Time Label');
end;

end.

Thanks in advance for any help with this matter. 在此先感谢您的帮助。

The problem is that after creating the label, I have to double click on the form before the double click event works on the label. 问题是创建标签后,我必须双击表单,然后双击事件才能在标签上起作用。

Assign LabelObject.OnDblClick when creating the label, ie inside the Button1Click event. 创建标签时,即在Button1Click事件内,分配LabelObject.OnDblClick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM