Controls and Parent

  • Thread starter Thread starter Zürcher See
  • Start date Start date
Z

Zürcher See

The Control class has the Parent property that is readonly.
When the control is added to a controls collection of another control the
Parent property refers to that control.
"Who" set the Parent property? How to implement this "mechanism"?

public abstract class MyControl
{
private MyControl parent;
public MyControl Parent{get{return this.parent;}}

private MyControlCollection controls;
public MyControlCollection Controls{get{return controls;}}
}

public class MyControlCollection : ArraList
{
protected new int Add(object value){return base.Add(value);}
public virtual Add(MyControl control)
{
//I think here schould be set the parent property, but how???
return this.Add(control);
}
}

Any help will be appreciated , thanks
 
I think this must be web Control? (Windows Forms control, Parent is not
read-only.)
If there is no set, then you can't change it. What do you really need to
accomplish? You could create your own writable property on MyControl to tell
which if any MyControlCollection it was in. Maybe that will serve your
purpose?

-Rachel
 
I don't want a writable property, I want to prevent such things like:

controls[0].Parent=new MyControl();

or

controls[0].Parent=null;
 
One possibility:

public abstract class MyControl
{
private MyControl parent;
public MyControl Parent{get{return this.parent;}}

private MyControlCollection controls;
public MyControlCollection Controls{get{return controls;}}

public MyControl()
{
controls = new MyControlCollection(this);
}

}

public class MyControlCollection : ArraList
{
protected MyControl owner;

public MyControlCollection(MyControl p)
{
owner= p;
}
protected new int Add(object value){return base.Add(value);}
public virtual Add(MyControl control)
{
control.Parent = owner;
return this.Add(control);
}
}



--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Zürcher See said:
The Control class has the Parent property that is readonly.
When the control is added to a controls collection of another control the
Parent property refers to that control.
"Who" set the Parent property? How to implement this "mechanism"?
Any help will be appreciated , thanks
 
You can't say control.Parent = owner if Parent is readonly (which seems to
be the case for web controls.)

Zuercher, I am assuming you've found out from experience that Adding a
control to a conrol collection automatically resets the conrol.Parent
property? I think then, that you could do something like:

Control SaveParent = MyControl.Parent;
SaveParent.Remove(MyControl); // not sure if this is necessary, but
shouldn't hurt
MyControlCollection.Add(MyControl);
SaveParent.Add(MyControl).

Then the most recent Add would be done to the "real" parent, so you would be
restoring the Parent property. Make sense? (I haven't tried it, it's just an
idea.)

-Rachel


James Curran said:
One possibility:

public abstract class MyControl
{
private MyControl parent;
public MyControl Parent{get{return this.parent;}}

private MyControlCollection controls;
public MyControlCollection Controls{get{return controls;}}

public MyControl()
{
controls = new MyControlCollection(this);
}

}

public class MyControlCollection : ArraList
{
protected MyControl owner;

public MyControlCollection(MyControl p)
{
owner= p;
}
protected new int Add(object value){return base.Add(value);}
public virtual Add(MyControl control)
{
control.Parent = owner;
return this.Add(control);
}
}



--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Zürcher See said:
The Control class has the Parent property that is readonly.
When the control is added to a controls collection of another control the
Parent property refers to that control.
"Who" set the Parent property? How to implement this "mechanism"?
Any help will be appreciated , thanks
 
Rachel is right; "Parent" is read-only. So, I played around with Lutz
Roeder's Reflector, and figured out how it's done. This trick is that
AddedControl can change the private member of the parameter, because both
are MyControls.

public class MyControlCollection : ArraList
{
protected MyControl owner;

public MyControlCollection(MyControl p)
{
owner= p;
}
protected new int Add(object value){return base.Add(value);}
public virtual Add(MyControl control)
{
owner.AddedControl(control);
return this.Add(control);
}
}

public abstract class MyControl
{
private MyControl parent;
public MyControl Parent{get{return this.parent;}}

private MyControlCollection controls;
public MyControlCollection Controls{get{return controls;}}

public MyControl()
{
controls = new MyControlCollection(this);
}

public void AddedControl(MyControl child)
{
child.parent = this;
}

}


--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Back
Top