connect procedure of a component to a procedure on the mainform

Hi,

I have made a component and I have the following problem:

How can I connect a procedure in the component with a procedure on
the form where the component is on. How can I do that?

This is the procedure in my TDSP3270-component:

procedure TDSP3270.DataOut(key: byte);
var
  obuf: string ;
  ba1, ba2: byte ;
begin
  ReadModified(obuf) ;
  rc2sba(fCsrRow,fCsrCol,ba1,ba2) ;
  obuf := Char(key) + Char(ba1) + Char(ba2) + obuf ;
  LockKeyBoard('SYSTEM',NOALARM) ;
  SendData(obuf) ;   //refering to procedure Senddata
end ;

This is the procedure on the main form:

procedure TForm1.SendData(sdata: string);
const
  C_CR    = $0A ; { carrage return }
  C_LF    = $0D ; { line feed }
type
  TOutBuf = array[0..4096] of char ;
var
  err: integer ;
  size: integer ;
  pc: PChar ;
  OutBuffer: TOutBuf ;
  i: integer ;
begin
  if EorFlag then
    sdata := sdata + Char(IAC) + Char(C_EOR) ;
  pc := @OutBuffer ;
  StrPCopy(pc,sdata) ;
  size := Length(sdata) ;
  err := send(soc,OutBuffer,size,0) ;
  if err = SOCKET_ERROR then
    ShowMessage('Send error: '
      +Format('%d',[WSAGetLastError()]))
  else
    begin
          for i:=0 to (size-1) do
            DumpChar(OutBuffer[i]) ;
          DumpFlush ;
    end ;
end ;

Maybe with a TNotifyEvent?

Greetings,

Peter Kiers


 
LVL 1
peterkiersAsked:
Who is Participating?
 
Russell LibbySoftware Engineer, Advisory Commented:
Event binding (not TNotify though, as you want to pass a string) would be a good way to go. This would allow you to bind the SendData process at runtime, which could then be called by the instance of your component class:

Eg:

type
  TOnSendData       =  procedure(Sender: TObject; Data: String) of object;
  TDSP3270         =  class(TComponent)
  private
     // Private declarations
     FOnSendData:   TOnSendData;
  protected
     // Protected declarations
  public
     // Public declarations
  published
     // Published declarations
     property       OnSendData: TOnSendData read FOnSendData write FOnSendData;
  end;

type
  TForm1            = class(TForm)
     procedure      FormCreate(Sender: TObject);
  private
     // Private declarations
  public
     // Public declarations
     procedure      OnSendData(Sender: TObject; Data: String);
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TDSP3270.DataOut(key: byte);
var obuf: string ;
     ba1, ba2: byte ;
begin
  ReadModified(obuf) ;
  rc2sba(fCsrRow,fCsrCol,ba1,ba2) ;
  obuf := Char(key) + Char(ba1) + Char(ba2) + obuf ;
  LockKeyBoard('SYSTEM',NOALARM) ;
  if Assigned(FOnSendData) then FOnSendData(Self, obuf);
end ;

procedure TForm1.FormCreate(Sender: TObject);
begin

  DSP3270.OnSendData:=Self.OnSendData;

end;

procedure TForm1.OnSendData(Sender: TObject; Data: String);
const
  C_CR    = $0A ; { carrage return }
  C_LF    = $0D ; { line feed }
type
  TOutBuf = array[0..4096] of char ;
var
  err: integer ;
  size: integer ;
  pc: PChar ;
  OutBuffer: TOutBuf ;
  i: integer ;
begin
  if EorFlag then
    sdata := sdata + Char(IAC) + Char(C_EOR) ;
  pc := @OutBuffer ;
  StrPCopy(pc,sdata) ;
  size := Length(sdata) ;
  err := send(soc,OutBuffer,size,0) ;
  if err = SOCKET_ERROR then
    ShowMessage('Send error: '
      +Format('%d',[WSAGetLastError()]))
  else
    begin
          for i:=0 to (size-1) do
            DumpChar(OutBuffer[i]) ;
          DumpFlush ;
    end ;
end;

end.


Regards,
Russell

0
 
2266180Commented:
i'm a little unclear on what you want to do. can you explain what you understand with the conenct thing? maybe give a clear example with steps
0
 
peterkiersAuthor Commented:
I want that a procedure in my component pass on a string to a procedure
on the form.

P.
0
 
2266180Commented:
well rllibby got the idea and gave the code :)
0
 
peterkiersAuthor Commented:
Yes, thank you ciuly.

Peter Kiers
0
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.