Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Writing a Component, Need Form Properties

Posted on 2006-06-23
3
Medium Priority
?
192 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 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

650 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