Recent

Author Topic: Changing a Control's Owner?  (Read 7952 times)

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Changing a Control's Owner?
« on: December 29, 2011, 03:16:19 pm »
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."
Lazarus Trunk / fpc 2.6.2 / Win32

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Changing a Control's Owner?
« Reply #1 on: December 29, 2011, 04:22:24 pm »
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

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing a Control's Owner?
« Reply #2 on: December 29, 2011, 04:39:42 pm »
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.
Lazarus Trunk / fpc 2.6.2 / Win32

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Changing a Control's Owner?
« Reply #3 on: December 29, 2011, 05:35:44 pm »
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

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Changing a Control's Owner?
« Reply #4 on: December 29, 2011, 05:48:28 pm »
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

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing a Control's Owner?
« Reply #5 on: December 29, 2011, 05:52:06 pm »
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;
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing a Control's Owner?
« Reply #6 on: December 29, 2011, 06:03:25 pm »
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?
Lazarus Trunk / fpc 2.6.2 / Win32

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Changing a Control's Owner?
« Reply #7 on: December 29, 2011, 06:36:17 pm »
   The Free method don not change the variable to nil.
   For instance:

Code: Pascal  [Select][+][-]
  1.   //Edit1 points to "h1234" memory address
  2.   Edit1.Free; // Releases Edit1's used memory but don't change it's value to nil
  3.   // Edit1 still points to "h1234" but there is no object there just garbage.
  4.  

here is the FreeAndNil code:
Code: Pascal  [Select][+][-]
  1.     procedure FreeAndNil(var obj);
  2.       var
  3.         temp: tobject;
  4.       begin
  5.         temp:=tobject(obj);
  6.         pointer(obj):=nil;
  7.         temp.free;
  8.       end;
  9.  

After you execute FreeAndNil(Edit1); Edit1 is = nil.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing a Control's Owner?
« Reply #8 on: December 29, 2011, 06:44:18 pm »
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.
Lazarus Trunk / fpc 2.6.2 / Win32

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Changing a Control's Owner?
« Reply #9 on: December 29, 2011, 07:14:49 pm »
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

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing a Control's Owner?
« Reply #10 on: December 29, 2011, 08:24:33 pm »
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.
Lazarus Trunk / fpc 2.6.2 / Win32

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Changing a Control's Owner?
« Reply #11 on: December 29, 2011, 09:20:53 pm »
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

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing a Control's Owner?
« Reply #12 on: December 29, 2011, 09:29:40 pm »
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.
Lazarus Trunk / fpc 2.6.2 / Win32

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Changing a Control's Owner?
« Reply #13 on: December 30, 2011, 02:07:48 pm »
I 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

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing a Control's Owner?
« Reply #14 on: December 31, 2011, 03:48:52 pm »
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.
Lazarus Trunk / fpc 2.6.2 / Win32

 

TinyPortal © 2005-2018