Solved

Writing a Component, Need Form Properties

Posted on 2006-06-23
3
188 Views
Last Modified: 2010-04-05
Hello,

I'm writing a component, well, trying to...

I'm working on a procedure that needs to populate local variables with properties of the form the component will be placed on.  it is not a visual component, TComponent class.

I need the Name property from the form (in string format)
I need the height, width, top, and left from the main form.

Thanks for your help.
0
Comment
Question by:fibdev
[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
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 50 total points
ID: 16973226

It basically boils down to walking the owner chain until you hit TCustom form, then read the form props. Example component below (does this in its Loaded event). For TComponent's (not inherited from TWinControl), the owner will normally be the form.

Regards,
Russell


////////////////////////////////////////////////////////////////////////////////
//   TTestComponent
////////////////////////////////////////////////////////////////////////////////
type
  TTestComponent        =  class(TComponent)
  private
     // Private declarations
     FTop:          Integer;
     FLeft:         Integer;
     FWidth:        Integer;
     FHeight:       Integer;
     FName:         String;
  protected
     // Protected declarations
     procedure      Loaded; override;
  public
     // Public declarations
     constructor    Create(AOwner: TComponent); override;
     destructor     Destroy; override;
  published
     // Published declarations
  end;

////////////////////////////////////////////////////////////////////////////////
//   Registration functions
////////////////////////////////////////////////////////////////////////////////
procedure Register;

implementation

//// TTestComponent ////////////////////////////////////////////////////////////
procedure TTestComponent.Loaded;
var  compOwner:     TComponent;
     formOwner:     TCustomForm;
begin

  // Check state
  if not(csDesigning in ComponentState) then
  begin
     // Get owner component
     compOwner:=Owner;
     // Walk the owner chain to get the form
     while Assigned(compOwner) do
     begin
        // Check component type
        if (compOwner is TCustomForm) then
        begin
           // Cast as form
           formOwner:=TCustomForm(compOwner);
           // Set internal variables
           FTop:=formOwner.Top;
           FLeft:=formOwner.Left;
           FWidth:=formOwner.Width;
           FHeight:=formOwner.Height;
           FName:=formOwner.Name;
           // Done
           break;
        end;
        // Get the next component in the chain
        compOwner:=compOwner.Owner;
     end;
  end;

end;

constructor TTestComponent.Create(AOwner: TComponent);
begin

  // Perform inherited
  inherited Create(AOwner);

  // Set defaults
  FTop:=0;
  FLeft:=0;
  FWidth:=0;
  FHeight:=0;
  SetLength(FName, 0);

end;

destructor TTestComponent.Destroy;
begin

  // Perform inherited
  inherited Destroy;

end;

//// Registration //////////////////////////////////////////////////////////////
procedure Register;
begin

  // Regsiter the component
  RegisterComponents('Additional', [TTestComponent]);

end;
0
 
LVL 28

Expert Comment

by:2266180
ID: 16973236
well .. that is pretty easy. just overwrite the create constructor and do the initialization there. like:

type tmycomponent = class(TComponent)
         constructor create(aowner:tobject); override;
       end;

constructor tmycomponent.create(aowner:tobject);
begin
  inherited;
  if aowner is tform then
    with aowner as tform do
    begin
      myleft:=left;
      etc
    end;
end;
0
 
LVL 28

Expert Comment

by:2266180
ID: 16973242
well .. never mind me :)
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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