Solved

component writing: events how to?

Posted on 1998-12-08
10
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 4

Expert Comment

by:BoRiS
ID: 1349802
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
ID: 1349803
mtieland

sorry replace this (every where you find it)
 FOnConnect

with this
 
FOnconnected

Later
BoRiS
0
 

Author Comment

by:mtieland
ID: 1349804
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:BoRiS
ID: 1349805
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
ID: 1349806
What if the API function already is an event?
0
 

Author Comment

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

Expert Comment

by:williams2
ID: 1349808
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
ID: 1349809
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
ID: 1349810
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
ID: 1349811
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
find a node in VST 2 92
how to resize animated Gif image in delphi ? 1 61
Add New Database Table in database file using delphi 3 54
Firemonkey Listview item popup menu ? 1 72
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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