Solved

Handle an event in the console application

Posted on 2004-04-13
8
1,142 Views
Last Modified: 2007-12-19
I'd like to using an Indy component in a console application.
As I cannot drag a component into a form, I created the conponent using

procedure Func(Sender: TObject; ......);
begin

end;

var UDPServ: TIdUDPServer;
begin
UDPServ:=TIdUDPServer.Create(nil);
UDPServ.OnUDPRead:=Func;           // is that right?
...
...
UDPServ.Free
end.

Can anyone give me an example how to handle the events in a component created dynamically.

thanks,

ysimon
0
Comment
Question by:ysimon
[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
  • 5
  • 2
8 Comments
 
LVL 7

Expert Comment

by:sftweng
ID: 10822637
You're on the right track but you need to ensure that the event handler conforms to the requirements (e.g., parameters). The easiest way to do this is to do a mock-up as a normal windowed application and then cut-and-paste the resulting framework (e.g., the OnUDPRead) event.
0
 

Author Comment

by:ysimon
ID: 10824626
It looks not that easy.
I have made the correct parameters in the event procedure.
I got an error message on

UDPServ.OnUDPRead:=Func;           // is that right?

incompatible type: 'method pointer and regular procedure'

How should I define that event procedure?
For a window application (with a Form), this statement is in an object definition located in the .dfm file.

ysimon
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10825701
Try:

procedure Func(Sender: TObject; AData: TStream;
      ABinding: TIdSocketHandle) of object;
begin
...
end {Func};
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!

 

Author Comment

by:ysimon
ID: 10828126
This doesn't work
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10829149
program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, IdUDPServer, IdSocketHandle;

type
  TMyUDPServer = class(TIdUDPServer)
  private
    procedure Func (Sender: TObject; AData: TStream;
                    ABinding: TIdSocketHandle);

  end;

var UDPServ: TMyUDPServer;

procedure TMyUDPServer.Func (Sender: TObject; AData: TStream;
                             ABinding: TIdSocketHandle);
begin
// Event handler code goes here
end;

begin
  UDPServ := TMyUDPServer.Create(nil);
  UDPServ.OnUDPRead := UDPServ.Func;           // is that right?
//...
//...
  UDPServ.Free
end.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10847306
ysimon, I tested this solution and it appears to work for me. Have you tried it?
0
 
LVL 7

Accepted Solution

by:
sftweng earned 50 total points
ID: 13190271
My solution met the need, as tested.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
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…

710 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