Solved

Writing a Component, Need Form Properties

Posted on 2006-06-23
3
181 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
  • 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:ciuly
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:ciuly
ID: 16973242
well .. never mind me :)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now