Control and Parent

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
 
S

Scott Mitchell [MVP]

Zürcher, you can use Reflector [http://www.aisto.com/roeder/dotnet/] to
view the source code of the .NET Framework BCL. If you look at the
System.Web.UI.Control class you'll see that it has an AddedControl()
method that is called from the ControlCollection class when the .Add()
method is called.

In AddedControl(), the code looks like:

protected internal virtual void AddedControl(Control control, int index)
{
if (control._parent != null)
{
control._parent.Controls.Remove(control);
}
control._parent = this;

...
}


As you can see, it takes in the Control to add as the first parameter,
and immediately sets the added control's private _parent property to
itself (namely, control._parent = this;)

hth

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
Z

Zürcher See

Yes, I have noticed the AddedControl method, but is protected, the
ControlCollection.Add method can't call it! Or I missing something?
 
S

Scott Mitchell [MVP]

To implement this yourself in a class, the class will likely have to
extend the System.Web.UI.Control class.

If you absolutely must twiddle with this private member variable
(_parent), you can do so using Reflection, but that will add a perf. hit
at runtime - see http://tinyurl.com/49z4n for info on setting a private
property using Reflection; see http://tinyurl.com/5nvr4 for a discussion
on the "cost" of Reflection.

hth


Zürcher See said:
Yes, I have noticed the AddedControl method, but is protected, the
ControlCollection.Add method can't call it! Or I missing something?

Zürcher, you can use Reflector [http://www.aisto.com/roeder/dotnet/] to
view the source code of the .NET Framework BCL. If you look at the
System.Web.UI.Control class you'll see that it has an AddedControl()
method that is called from the ControlCollection class when the .Add()
method is called.

In AddedControl(), the code looks like:

protected internal virtual void AddedControl(Control control, int index)
{
if (control._parent != null)
{
control._parent.Controls.Remove(control);
}
control._parent = this;

...
}


As you can see, it takes in the Control to add as the first parameter, and
immediately sets the added control's private _parent property to itself
(namely, control._parent = this;)

hth

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!


--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top