0

I have created a simple Delphi form with a button that, when pressed, creates a label object in run time. 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.

4
  • 1
    Assign LabelObject.OnDblClick inside the Button1Click event. Commented Jul 6, 2016 at 6:48
  • @LURD perfect, thank you so much! Commented Jul 6, 2016 at 7:04
  • If you assign the double click handler for the label inside the double click handler for the form, then you should not be surprised that the label only reacts to a double click after you double clicked the form. Commented Jul 6, 2016 at 7:10
  • @RudyVelthuis yeah that makes sense now, silly mistake haha. But learning all the time! Thanks so much for the explanation. Commented Jul 6, 2016 at 7:37

1 Answer 1

5

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, i.e. inside the Button1Click event.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.