views:

51

answers:

2

Hopefully I am stating that right. I have a WinForm(3.5) app that has 1 Form that is broke into two regions. 1 is the Navigation and the other, a Panel, is the Content. You select what you want in the Navigation Portion, i.e. Demographics, and then it embeds a UserControl containing all the Demographics controls in the Panel.

What I am asking is if each User Control should have a Property for each Control on it. Example: ucDemographics has a textbox, named txtCity. Should there be a Property to store the value of txtCity and allow my Form and other User Controls to access it?

Is that the generally accepted 'Best Practice'?

If not, what is?

+5  A: 

It depends on what you want to achieve with your UserControl.

Normally you wouldn't expose the txtCity because the caller could manipulate everything about the textbox then. In most scenarios, you would only expose the current text á la

public string CityText
{
    get { return this.txtCity.Text; }
}
herzmeister der welten
I see, so normally you would only use a Get, not a Set or just not a Public Set? That way the User Control itself can use it to set the variable. Am I missing the point?
Refracted Paladin
Depends on the purpose of your UserControl. If you want to allow the host form to update the text, then expose a public set, otherwise don't.
Christian Hayter
+2  A: 

No, that's not really a best practice. The intention of a user control is to compose a new control with its own behavior. You should at most have "several" properties, methods and events that are public and allows a form to interact with the new control. If you find that the only good way to work with it is by exposing its constituent controls that you're better off not using a UserControl but just place the controls on the form directly.

Hans Passant