When self is the owner, it is responsible for freeing itself. 
It's the same concept as when you create a form at runtime:

Form1 := Tform.create(self);
Form1.Showmodal;
Form1.Free;
Form1 := nil;

If the component was owned by something else, then you wouldn't have to worry 
about it, the owner would free it. This is also analagous to when you create a 
secondary form owned by mainform:

Form1 := TForm.create(mainform);
Form1.showmodal;
//don't free it, mainform will free it when it shuts down.

Not freeing it would produce a memory leak.

Dave


[EMAIL PROTECTED] wrote:                               Last try here.
 
 If you have ever bought one of those 40 pound volumes "Delphi Developer's
 Guide" by Teixeira and Pacheco (Delphi 4,5,6) you can find the source for
 this in the chapter on creating custom components. This is what they call
 a "container component". Decending from TCustomControl (or TWinControl, I
 tried both), it has an edit box and a button. Not complicated stuff.
 
 Three questions.
 
 1. Why do they free the edit control and the button in the destructor? If
 these "contained components" are created with Owner=self, where is the
 need to free them?
 
 2. The component works fine. Assume it is named FOO, either in the
 designer or in code. The normal way of naming components. Then the test
 "if ActiveControl=FOO" always fails. I can see the name property is not
 set. Yet it has a name and the IDE handles it fine.
 
 This is a head-scratcher for me. Complete failure to understand something
 very basic.
 
 3. This is my third try in this forum. So I am not phrasing the question
 well, or I am asking in the wrong place. Yet when I google for component
 writing groups or forums, all I can find is dead links. Is anyone aware of
  group that is active, with help for basic component writing?
 
 I found a professional outfit willing to try and answer for some big
 bucks. I must go with them unless I can find help here.
 
 Thanks in advance.
 
 =========================================================
 unit imMyControl;
 {
   Simple test 'container component' appears to work but
   fails to have a name property (or otherwise be detected as ActiveControl).
 }
 interface
 
 uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   StdCtrls, Buttons,Mask;
 
 type
 
 //=========================================
   { Later we will want to tweak keystroke handling for masked edits.
   }
   //TImsCustomMaskEdit = class(TCustomMaskEdit)
   //end;
 
 //=========================================
   { Base class
   }
   TImsCustomMyControl = class(TWinControl)
     constructor Create(AOwner: TComponent); override;
     destructor  Destroy; override;
 
 private
     FEdit:TEdit;
     FSpeedButton: TSpeedButton;
 
 procedure ButtonClick(Sender:TObject);
     function  GetText:string;
     procedure SetText(Value:string);
 
 protected
     procedure WMSize(var Message: TWMSize); message WM_SIZE;
 
 public
     procedure SetFocus;override;
     property Text:string read GetText write SetText;
   end;
 
 //=========================================
   { Lookup control
   }
   TImsMyControl = class(TImsCustomMyControl)
   published
     property Align;
     property Enabled;
     property Font;
     property OnEnter;
     property OnExit;
     property Text;
     property TabOrder;
     property TabStop;
     property Visible;
   end;
 
 procedure Register;
 
 implementation
 
 procedure Register;
 begin
   RegisterComponents('TEST', [TImsMyControl]);
 end;
 
 
//==============================================================================
 {
 }
 constructor TImsCustomMyControl.Create(AOwner: TComponent);
 begin
   inherited Create(AOwner);
 
 // An edit contro
   FEdit:=TEdit.Create(self);
   FEdit.Parent:=self;
   FEdit.Height:=21;
 
 // A button
   FSpeedButton := TSpeedButton.Create(Self);
   FSpeedButton.Left := Width;
   FSpeedButton.Height := 19; // two less than TEdit's Height
   FSpeedButton.Width  := 19;
   FSpeedButton.Caption := 'F';
   FSpeedButton.Parent := Self;
   FSpeedButton.OnClick := ButtonClick;//MyDropDown;
 
 Width  := Width+FSpeedButton.Width;
   Height := FEdit.Height;
 end;
 
 destructor  TImsCustomMyControl.Destroy;
 begin
   // Curious. Why free these? Are not owned components to be freed
 automagically?
   FEdit.Free;
   FSpeedButton.Free;
   inherited Destroy;
 end;
 
 { Maintain edit control and button in their respective places
 }
 procedure TImsCustomMyControl.WMSize(var Message: TWMSize);
 begin
   inherited;
   FEdit.Width := Message.Width-FSpeedButton.Width;
   FSpeedButton.Left := FEdit.Width;
 end;
 
 
//==============================================================================
 { Delegate to edit control
 }
 function TImsCustomMyControl.GetText:string;
 begin
   Result:=FEdit.Text;
 end;
 procedure TImsCustomMyControl.SetText(Value:string);
 begin
   FEdit.Text:=Value;
 end;
 procedure TImsCustomMyControl.SetFocus;
 begin
   FEdit.SetFocus;
 end;
 
 
//==============================================================================
 {
 }
 procedure TImsCustomMyControl.ButtonClick(Sender:TObject);
 begin
   FEdit.SetFocus;
   ShowMessage('click');
 end;
 
 { -Compile and install.
   -Drop one on a new form. Compile and run.
   -Try to report the name of the control, or test for it when it is
 ActiveControl!
 }
 end.
 
 
     
                               

       
---------------------------------
Boardwalk for $500? In 2007? Ha! 
Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.

[Non-text portions of this message have been removed]

Reply via email to