VCL events with anonymous methods - what do you think about this implementation?

后端 未结 3 1475
梦谈多话
梦谈多话 2021-02-13 03:57

Since anonymous methods appeared in Delphi I wanted to use them in VCL components events. Obviously for backward compatibility the VCL wasn\'t updated, so I managed to make a si

3条回答
  •  我在风中等你
    2021-02-13 04:36

    You can take a look at my multicast event implementation in DSharp.

    Then you can write code like this:

    function NotifyEvent(Owner: TComponent; const Delegates: array of TProc): TNotifyEvent; overload;
    begin
      Result := TEventHandler.Create>(Owner, Delegates).Invoke;
    end;
    
    function NotifyEvent(Owner: TComponent; const Delegate: TProc): TNotifyEvent; overload;
    begin
      Result := NotifyEvent(Owner, [Delegate]);
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Button1.OnClick := NotifyEvent(Button1, [
        procedure(Sender: TObject)
        begin
          Caption := 'Started';
        end,
        procedure(Sender: TObject)
        begin
          if MessageDlg('Continue?', mtConfirmation, mbYesNo, 0) <> mrYes then
          begin
            Caption := 'Canceled';
            Abort;
          end;
        end,
        procedure(Sender: TObject)
        begin
          Caption := 'Finished';
        end]);
    end;
    

提交回复
热议问题