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
Solved

component writing: events how to?

Posted on 1998-12-08
10
218 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
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 95
Intraweb download file link ? 1 155
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 71
enhance the following code 3 37
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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