Recent

Author Topic: How to create a control programatically ?  (Read 4252 times)

newman

  • Jr. Member
  • **
  • Posts: 75
How to create a control programatically ?
« on: October 28, 2010, 03:20:32 am »
Hi,

I am using the 5dpo serial port control for my applications. I have developed a series of procedures/functions for a proprietary RS 485 based protocol. I would like to put all these procedures/functions into a single unit, called micrors485.pas. The problem is that these modules use the 5dpo serial port control (the control is drawn on a form). Also the receive event of the control is also used.

How do I create a control on the micrors485.pas unit ?? Also how do I add the event handler for the control into this unit ??

I can post more information if required.

thanks
nm

Platform: Windows XP

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to create a control programatically ?
« Reply #1 on: October 28, 2010, 03:52:41 am »
Quote
How do I create a control on the micrors485.pas unit ??
Like this:
Code: [Select]
var
  MyControl: TMyControl;
begin
  MyControl := TMyControl.Create(<constructor arguments>);
  MyControl.X := ... ; // set up its properties
Quote
Also how do I add the event handler for the control into this unit ??
First, create the event handler:
Code: [Select]
interface

type
  TMyControl = class
    ...
    procedure SomeProc;
    ...
  end;

implementation

...

procedure TMyControl.SomeProc;
begin
  ...
end;

...
Then assign the handler in the constructor:
Code: [Select]
constructor TMyControl.Create;
begin
  TMyControl.OnProc := @SomeProc;
end;

ivan17

  • Full Member
  • ***
  • Posts: 173
Re: How to create a control programatically ?
« Reply #2 on: October 28, 2010, 05:58:28 am »
lets be a little more precise, shall we... (reused previous answer)

Quote
How do I create a control on the micrors485.pas unit ??
Like this:
Code: [Select]
 MyControl: TMyControl;    <<< add this into main form's public section
Code: [Select]
 ....                                      //      this goes into form's oncreate;
  MyControl := TMyControl.Create(Self);   <<  self means main form;
                        // main form will destroy your control when itself is destroyed
  MyControl.Name := 'newman' ; // set up its properties
  MyControl.Parent := Self;        <<<<<<    do NOT forget to set the parent;
               /// parent could also be Panel3 or TabSheet2 or GroupBox1...
  MyControl.Left := 8 ; // you could use SetBounds here
  MyControl.OnMyEvent := @SomeProc;     // see below

Quote
Also how do I add the event handler for the control into this unit ??
First, create the event handler:
Code: [Select]
procedure SomeProc(...);    << this goes in form's public area where the REST of events are
...
implementation
...
...

procedure TMainForm.SomeProc(...);  // yes, make it a method belonging to the form
begin
  ...
end;

...

newman

  • Jr. Member
  • **
  • Posts: 75
Re: How to create a control programatically ?
« Reply #3 on: October 28, 2010, 06:17:23 am »
Thanks for the information, I will try this out.

 

TinyPortal © 2005-2018