Please see my comment to the question. I don't see a problem.
However, maybe your problem is the one typical for other members who asked question on related topics.
The problem is: the child controls of your controls are not directly visible to the code using your control. The only exposed property is
Control.Controls
, but it does not use strong typing for every members, so it's now so useful.
One option is to expose each child control instance you need to access as a property of your control, but I would not recommend it. This is certainly the easiest way, but it violates the isolation of implementation detail of your user control, that is, the encapsulation. This simple technique can be used in simple applications though.
More refined approach is to add some properties and events to your user control, only the functional ones. In the user control class implementations, they can be implemented using the child controls. For example, let's assume you have two child controls to access in a limited way:
Label MyLabel
should be accessible only to change its background color, and
GridView GridView
should respond to
RowCommand
event via the handler setup by the user of your user control. It could be something like this:
partial public class MyControl {
GridView GridView = new GridView();
Label MyLabel = new Label();
public MyControl() {
GridView.RowCommand += (sender, eventArgs) => {
if (this.RowCommand != null)
this.RowCommand.Invoke(this.GridView, new GridViewCommandEventArgs());
};
}
public Color MyLabelBackColor { get { return MyLabel.BackColor; } set { MyLabel.BackColor = value; } }
public event System.EventHandler<GridViewCommandEventArgs> RowCommand;
}
Got the idea?
[EDIT]
In many cases, you would need to make a step forward and re-package event argument of the event exposed to the user of the user control in a different event argument class which you can subclass from
System.EventArgs
or other most suitable event argument type. The problem is: if you assume that the access to the instance of the grid view is needed for a user to implement the even handler, the only reference is the
sender
parameter. This is not a string typing, will require type case, and, besides, it exposes a private user control member. Therefore, you can use the (generic) event type based on your own event args type, to provide the user with the information this user will really need. The code sample I've shown above will be nearly the same, only
/* ... */
part will submit some really useful information and the event argument type will be different, a customized one.
—SA