Solved

Writing a Component, Need Form Properties

Posted on 2006-06-23
3
186 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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