I'm probably in a hurry and missing something obvious, but I'm having
trouble encapsulating everything I need to in a user control.

Suppose I have a UserControl consisting of 3 textboxes (First, Middle, and
Last Name, for instance).  I have nice layout and validation logic
contained within the control.  It is supposed to be a black box, a closed
component.

As a simple first example, I use proper encapsulation techniques to expose
3 properties  (FirstNameText, MiddleNameText, and LastNameText) that access
the Text properties of the 3 textbox controls contained in the
usercontrol.  I would certainly expose a few more later, but this will
suffice for example.  I DO NOT expose 99% of the properties about the
textboxes on purpose, of course.

Now, the darn Controls collection destroys all that by giving the parent
form all the access in the world to the constituent textboxes on the
usercontrol.  Why bother having "private" as the default modifier for
controls if you open the door to chaos with the Controls collection? I
can't turn it off.

Heck, the parent form can Add, Remove, or even worse Clear the Controls
collection of the user control.  This sucks badly.

I have tried to use name hiding to change the level of the Controls
collection to private in the UserControl so that the UserControl itself
could reference it as this.Controls instead of always having to use
base.Controls.

//following code in the UserControl is useless
[Browsable(false),
EditorBrowsable(EditorBrowsableState.Never)]
new private Control.ControlCollection Controls
{
  get
  {
    return base.Controls;
  }
}

This was to no avail..it still is public accessible to the outside world.
The form that is hosting the UserControl can still get to the collection
and screw around with the individual textboxes within the UserControl.

Heck, not only is the Controls collection still public to the hosting form,
but the stupid EditorBrowsable property seems to be ignored and you can see
the Controls collection member in IntelliSense when you hit the dot.

So, does anyone know how to hide this stinking collection so you can
actually make a stable component that doesn't expose it's innards to
parents that consume it?

This is chaos if you can't turn it off.  Why bother with those private
controls and good practice accessors, forget OOP and just expose the whole
friggin world as public like in old VB if you have to have a public
Controls collection always.

There has to be a way to secure your constituent controls, hasn't there?

I hope I'm missing the forest for the trees or something here.

Thanks,

Jeff

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to