Changing a Control's Owner? lazarus / 31-LCL ---------------------------------------------------------------------- Avishai · 2011-12-29 14:16 After a control is created and the Owner is assigned, is there a way to change the Owner?  I tried "Label1.Owner:= Edit1;" but it said that "No member is provide to access property." ---------------------------------------------------------------------- User137 · 2011-12-29 15:22 You can try label1:=TLabel.Create(edit1) and then setting its properties manually. I just guess that TEdit is not a "container" object that can take child objects. ---------------------------------------------------------------------- Avishai · 2011-12-29 15:39 Thanks User137. What I am doing is writing 2 components, TStickyLabel and TStickyBtn.  TSickyLabel has a property Associate and uses AnchorToCompanion.  This allows it to "Stick" to any TWinControl similar to TLabeledEdit.  Right Now if the Associated Control is deleted at Design-Time, the Label stays.  I was thinking maybe to delete both, but the Owner is assigned when it is created.  If I can change the Owner to the Associated control, I think it should delete both cleanly.  There are other ways of course. TStickyBtn is the same but comes from TSpeedButton.  This allows me to add a TSpeedButton to any TWinControl similar to TEditButton. Both work fine, it's just what to do when the Associated controls are deleted. ---------------------------------------------------------------------- garlar27 · 2011-12-29 16:35 AFAIK the "container thing" is to show other controls inside ... but the ownership means that the owner takes responsibility over the life cycle of the owned (some thing like I'm gonna free Label1 when destroying). The Parent is the one that is responsible of where the component is showed and the tab order it has, to disable or enable Label1 when the parent is enabled/disabled. I think that you should Create a Clone of Label1 to a different Owner and then free Edit1 (which will free Label1). or even better create Edit1 and Label1 with the same owner (lets say its Panel1) and associate them with anchors. If you need to free Edit1 you just free it and associate Label1 to a new control or change the parent. ---------------------------------------------------------------------- felipemdc · 2011-12-29 16:48 You don't need to use the Owner, you can set it to nil when creating the control. But then you need to free it yourself. ---------------------------------------------------------------------- Avishai · 2011-12-29 16:52 This is what I am doing now with both controls.  I haven't figured out how to do a "FreeAndNil".  FreeAndNil(What?). procedure TCustomStickyLabel.Notification(AComponent: TComponent;   Operation: TOperation); begin   inherited Notification(AComponent, Operation);   if (Operation = opRemove) and (AComponent = FAssociate) then begin     Anchors:= [akLeft,akTop];      FAssociate:=nil;   end; end; ---------------------------------------------------------------------- Avishai · 2011-12-29 17:03 Thank you Mr. felipemdc.  I just did a check and having the Owner set to the Associated control and then deleting the Associated control does not seem to delete both "Cleanly" as I thought. TStickyLabel gets deleted as expected, but it is not NIL.  I am not sure what this does to memory.  Does it just become trash?  Is it Safe?  Maybe I should call the Destructor at the end of TCustomStickyLabel.Notification? ---------------------------------------------------------------------- garlar27 · 2011-12-29 17:36 The Free method don not change the variable to nil.    For instance: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   //Edit1 points to "h1234" memory address  Edit1.Free; // Releases Edit1's used memory but don't change it's value to nil  // Edit1 still points to "h1234" but there is no object there just garbage.  here is the FreeAndNil code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     procedure FreeAndNil(var obj);      var        temp: tobject;      begin        temp:=tobject(obj);        pointer(obj):=nil;        temp.free;      end;  After you execute FreeAndNil(Edit1); Edit1 is = nil. ---------------------------------------------------------------------- Avishai · 2011-12-29 17:44 Thanks garlar27, but when Edit1 is deleted at design-time I want to delete TStickyLabel as well.  (TStickyLabel.Associate:= Edit1;)  I think my explanation is not so clear. 1. Edit1 is added at design-time. (Edit1 is TEdit) 2. StickyLabel1 is added at design-time. 3. StickyLabel1 is associated to Edit1. 4. Changed my mind.  I delete Edit1. 5. I want StickyLabel1 to be deleted also. ---------------------------------------------------------------------- garlar27 · 2011-12-29 18:14 I think I get it!!! You are building a component ain't you? I think it don't have sense to add 2 separate components and then remove 1 and pretend that removing Edit1 cause an automatic remove of StickyLabel1. For me, as I understand it's like: 1. Table1 is added at design-time. 2. DataSource1 is added at design-time. 3. DataSource1 is associated to Table1. 4. Changed my mind.  I delete Table1. 5. I want DataSource1 to be deleted also. (it doesn't make sense) If you want to do something like that, I think that TStickyLabel should be a container like TPanel. ---------------------------------------------------------------------- Avishai · 2011-12-29 19:24 Thank you garlar27.  The problem with using a container is that I either end up having to write a very complicated Control to allow different types of TWinControls and how to handle each one, or I have to write a component for each TWinControl.  By simply Anchoring to a TWinControl it makes things very simple.  My first attempt was to use a TCustomControl as a container but it got very complicated from the very start.  What I have now works very well.  I just thought it would make sense if I could delete both controls. ---------------------------------------------------------------------- garlar27 · 2011-12-29 20:20 I've used TLabeledEdit and TEditButton and what I didn't like of those control was the anchor thing since it don't have an acceptable behavior. I guess you know what  I mean... I think that the key of the problem is that TStickyLabel take control over how the anchor behaves, and provide a practical solution. some things I can think of:    o- How the StickyLabel glues to the sticked control (relative position and margin between Sticky and sticked Control).    o- How this group anchors to other controls (treat them like one component). Using a container may help.    o- If you use a Container: when associating the controls you can get the size, position and Control's anchors, move sticky to that position set size to the Control's one + the label space. Then change Control.Parent to StickyLabel and set control position in the proper place and anchors to fit the sticky config. (EDIT: I think the container must be transparent). You don't need to create a separated class for each control type (neither I think that this is what you want).   Sorry for my English. I never know if I write what I exactly wanted to say... ---------------------------------------------------------------------- Avishai · 2011-12-29 20:29 garlar27, Never apologize for not speaking a language well unless it is your mother's tongue.  I understand everything you say. I have already tried the container approach and it got too complicated very fast. ---------------------------------------------------------------------- garlar27 · 2011-12-30 13:07 Quote from: Avishai on December 29, 2011, 09:29:40 pmI have already tried the container approach and it got too complicated very fast. Maybe this is why there ain't many controls like this. AFAIR Eny was working in something similar... maybe he can help you. ---------------------------------------------------------------------- Avishai · 2011-12-31 14:48 If anyone would like to try TStickyLabel and TStickyBtn, I have attached the file.  Example: 1. Add a TEdit to a Form. 2. Add a TStickyLabel to the Form. 3. Set TStickyLabel.Associate to the TEdit. TStickyLabel.LabelSpacing and TStickyLabel.Position can be set also.