?
Solved

component writing: events how to?

Posted on 1998-12-08
10
Medium Priority
?
236 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
Independent Software Vendors: 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 210 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

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