?
Solved

Writing a Component, Need Form Properties

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

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 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