Solved

component writing: events how to?

Posted on 1998-12-08
10
201 Views
Last Modified: 2010-04-04
Can anyone explain me in detail how te create costom and non-costom events in a component?
For instance I want to make a RAS component that triggers an event when a connection is made...
0
Comment
Question by:mtieland
  • 4
  • 3
  • 3
10 Comments
 
LVL 4

Expert Comment

by:BoRiS
Comment Utility
mtieland

here is one way to do it....

private
 FOnConnect: TNotifyEvent;
 procedure Connected(Value: Boolean)

published
 property OnConnect: TNotifyEvent read FOnConnect write FOnConnect;

then in the code...

procedure TRasConnection.Connected(Vaule: Boolean);
begin
   if value <> FOnConnected then FOnConnected := value;
    if Value then
     begin
      ShowMessage('You have Connected');
        if Assigned (FOnConnected) then FOnConnected(Self);
 end;
end;

I have not tested this code, I just wrote here quickly to give you the basic idea of the Events structure, this may or may not work(the code)but you can get the idea...

Later
BoRiS
0
 
LVL 4

Expert Comment

by:BoRiS
Comment Utility
mtieland

sorry replace this (every where you find it)
 FOnConnect

with this
 
FOnconnected

Later
BoRiS
0
 

Author Comment

by:mtieland
Comment Utility
Can you give me an example how to make an event that responds to a windows api event (There should be a api event that is triggerd when ras is connected)

Thanx
0
 
LVL 4

Expert Comment

by:BoRiS
Comment Utility
mtieland

use the same as above for API then just call the API in the event handler...

procedure TRasConnection.Connected(Vaule: Boolean);
       begin
          if value <> FOnConnected then FOnConnected := value;
           if Value then
            begin
             ShowMessage('You have Connected');
              RasGetConnection(blahblah);
               RasGetModemStatus(blahblah);
               if Assigned (FOnConnected) then FOnConnected(Self);
        end;
       end;

just remeber to add the correct files to the uses clause...
uses
Windows, Forms, ..., winsock;

Later
BoRiS
0
 

Author Comment

by:mtieland
Comment Utility
What if the API function already is an event?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:mtieland
Comment Utility
What I mean is can you enscapulate a already existing system event into your own component...?
0
 
LVL 3

Expert Comment

by:williams2
Comment Utility
Yes you can!

Every event is build on the windows messagesystem provided with a specific code like CM_MOUSEENTER. The prettiest way to do this is like this:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FOnMouseEnter: TNotifyEvent;
    FOnMouseLeave: TNotifyEvent;
    Procedure CMMouseLeave(var Msg: TMessage); Message CM_MOUSELEAVE;
    Procedure CMMouseEnter(var Msg: TMessage); Message CM_MOUSEENTER;
  public
    //Test procedures
    procedure OnMyMouseEnter(Sender: TObject);
    procedure OnMyMouseLeave(Sender: TObject);
  //published
    //If you are to write components, this section should be published
    property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
    property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;

  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Procedure TForm1.CMMouseEnter(var Msg: TMessage);
Begin
  If Assigned(FOnMouseEnter) then FOnMouseEnter(Self);
  inherited
End;

Procedure TForm1.CMMouseLeave(var Msg: TMessage);
Begin
  If Assigned(FOnMouseLeave) then FOnMouseLeave(Self);
  inherited
End;

procedure TForm1.FormCreate(Sender: TObject);
begin
  OnMouseEnter:= OnMyMouseEnter;
  OnMouseLeave:= OnMyMouseLeave;
end;

procedure TForm1.OnMyMouseEnter(Sender: TObject);
begin
  Label1.caption:= 'Inside!';
end;

procedure TForm1.OnMyMouseLeave(Sender: TObject);
begin
  Label1.caption:= 'Outside!';
end;


end.
0
 
LVL 3

Expert Comment

by:williams2
Comment Utility
Please note:

The procedure:

    Procedure CMMouseLeave(var Msg: TMessage); Message CM_MOUSELEAVE;

Needs to be named like the message_id, else it won't work.

Regards,
Williams
0
 

Author Comment

by:mtieland
Comment Utility
Thanx Williams please perpose your comment as an answer to get the points!

Martijn
0
 
LVL 3

Accepted Solution

by:
williams2 earned 70 total points
Comment Utility
That's allright my friend, I'm glad it could be to any use for you. If you need to know more, just say so.

Regards,
Williams
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now