picture

TNotifyEvent. OnClick Example for Delphi.

Listening to events in Delphi is pretty easy. In following I’d like to introduce two ways to list to events. Consider following example:


type
  TForm1 = class(TForm)
    MyButton1: TButton;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure MyBtnClick(Sender: TObject);
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MyButtonClick(Sender: TObject);
begin
  ShowMessage('Button is clicked!');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyButton1 := TButton.Create(Form1);
  MyButton1.Parent := Form1;
  MyButton1.OnClick := MyButtonClick;
end;

In this simple case MyButtonClick is a class method. The way it’s used in the example above is quite usual. But what to do if you need to use some method to handle OnClick event, which is defined outside the class(external function)? In this situation you have to wrap up your method by a record:


type
  TMethodPointer = packed record
    pMethod: Pointer;
    pObject: TObject;
end;

After this you can add your event handler, using casting to TNotifyEvent like this:


methodPointer.pMethod := @MyOnClickMethod;
methodPointer.pObject := nil;
MyButton1.OnClick :=  TNotifyEvent(methodPointer);


Comments to the Post

  • Fine article. There’s a lot of effective data here, though I did want to let you know something – I am running Fedora with the circulating beta of Firefox, and the design of your blog is kind of flaky for me. I can read the articles, but the navigation doesn’t function so great.

    author: American Silver Eagles | May 23rd, 2010 at 7:37 pm
  • hey whats your myspace page.

    author: how much should i weigh | June 20th, 2010 at 12:15 am
  • Intimately, the post is in reality the greatest on this deserving topic. I fit in with your conclusions and will thirstily look forward to your next updates. Just saying thanks will not just be enough, for the phenomenal lucidity in your writing. I will directly grab your rss feed to stay privy of any updates. Authentic work and much success in your business efforts!

    author: Junk Silver | July 3rd, 2010 at 9:40 pm
  • Hey I just wanted to let you know, I actually like the writing on your site. But I am employing Chromium on a machine running version 8.x of Crashbang Ubuntu and the design aren’t quite right. Not a big deal, I can still essentially read the articles and research for information, but just wanted to inform you about that. The navigation bar is kind of hard to use with the config I’m running. Keep up the good work!

    author: Silver Bullion | July 3rd, 2010 at 10:10 pm
  • Anti Adware…

    best Anti Adware software free download for microsoft windows….

    author: Anti Adware | July 4th, 2010 at 5:51 am
  • Only want to say your article is stunning. The clarity in your post is simply striking and i can assume you are an expert on this field. Well with your permission allow me to grab your rss feed to keep up to date with incoming post. Thanks a million and please keep up the admirable work.

    author: American Silver Eagle coins | July 4th, 2010 at 5:01 pm
  • Not often we can find cool theme for wpress. There are enough good themes, but sometimes inconvenient to set up. I have installed last versions of MASUGID, My Office, Red Steel, DesignPile, also I have installed also BAsense – simple, but made for adsense. Infrequent we can find something cool but good in settings.

    author: Scrap Gold Prices | July 14th, 2010 at 12:19 pm

Leave a Reply

For spam detection purposes, please copy the number 5279 to the field below: