How do I programatically create Indy components

Hi experts, I am trying to create a service and creating the indy components (tcpserver, udpserver) from code rather than dropping a component on a form.  I'm looking for some code sample that shows how to do this.

Who is Participating?
Geert GConnect With a Mentor Oracle dbaCommented:
GExperts has a very nice feature for this
after installing it into the Delphi IDE, do this:
put a component on your form as you normally do
then right click on the component
(gexperts will have added extra menu options)
Components to Code

This creates a variable, the component creation with the constructor and the properties

I got this, from 3 clicks and a Ctrl-V to paste:

  IdTCPClient1: TIdTCPClient;

  IdTCPClient1 := TIdTCPClient.Create(Self);
  with IdTCPClient1 do
    Name := 'IdTCPClient1';
    MaxLineAction := maException;
    ReadTimeout := 0;
    Port := 0;

ThievingSixConnect With a Mentor Commented:

procedure Button1Click(Sender: TObject);
  IRC : TidIRC;
  IRC := TidIRC.Create(...);
  blah blah

Remember to include the correct units associated with the component in your uses for it to work.
Hey, that's really useful!

Thanks Geert_Gruwez =D
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

viziopointAuthor Commented:
The GeExpert tip is indeed a very really useful tip! - Thanks!

I'm getting some odd results though like:

Bindings := <>;
It tried to replace the <> with nil - compiles but does not work (crashes) so I just commented them out and it seems to work.

I also had problems with assigning event handlers since I don't have a form.  I was able to fix this with a little trick that I think is fine:

   TEventHandlers = class // create a dummy class
       procedure UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
   EvHandlers : TEventHandlers;
IdUDPSever1.OnUDPRead := EvHandlers.UDPServerUDPRead

to the component created with the GeExpert code and it seems to work.

I'll continue to test and report back.

Thanks again for the help.
Geert GOracle dbaCommented:
you don't have a form and you want to use an event handler ?

the way you do it with the dummy class is actually the solution!

why you may ask ... well because how the event handler is defined
a event handler is a method, and a method is a procedure of a object

TNotifyEvent = procedure (Sender: TObject) of object;

the *of object* at the end means it must be defined inside a class.

you will have to create a instance of EvHandlers at runtime too...

Actually you should have the indy component in the event handler dummy class.
viziopointAuthor Commented:
Just wanted to confirm that following the tips above, everything worked fine.  Thanks Again!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.