Roopica wrote:
"this control needs to behave diffrently when invoked through different sources."
1. need to know exactly what you mean by "
invoked" here: do you mean: added to a Form, or other Container Control
at design-time, or created by a Form, and added, at
run-time ?
Remember that
at design-time, or, at run-time, you could be adding the UserControl to: either a Form, or to some other Container Control, like a Panel, on a Form, or: to some oomplexly nested series of Container Controls, for example: into a Panel within a Panel on a Form !
2. you need to describe exactly what "
behave differently" means: to change certain of the UserControl's properties, variable value, or EventHandlers for Controls ... or, ?
RyanB's comment on your question: proposing the UserControl have a variety of different possible facilities (methods ? event-handlers ? variables ? properties ?) that different host containers can make use of: I believe is not optimal; I believe it violates the principle of "separation of concerns." It creates specific dependencies between the UserControl, and specific "hosts."
Far more desirable, imho, is to have the UserControl "publicly" expose its internal objects through public properties, which it's possible different "hosts" can then modify state, properties, or event-handlers of.
For example: I have a button named 'FirstButton' in my UserControl: in the 'Load EventHandler of this UserControl, I have some code like this:
public Button FirstButton { get; set; }
private void UserControl1_Load(object sender, EventArgs e)
{
FirstButton = this.button1;
}
Now every time an instance of UserControl1 is created, the "host" can access the button, through its public Property, "FirstButton," and change it's properties: but we have to consider two cases:
1. the user at design time hss already placed the UserControl on the Form by drag-drop: in that case we can use the Form Load EventHandler like this:
FirstUserControl frmUC;
private void Form1_Load(object sender, EventArgs e)
{
frmUC = this.Controls["firstUserControl1"];
if(frmUC != null)
{
FirstUserControl firstUC = frmUC as FirstUserControl;
Button theButton = firstUC.FirstButton
theButton .Click += FirstButton_Click;
firstUC.BackColor = Color.WhiteSmoke;
theButton.BackColor = Color.MintCream;
theButtonClick += click1;
}
}
2. The user is adding an instance of the UserControl at run-time: here we can use the Form 'ControlAdded EventHandler as our notification mechanism:
private void Form1_ControlAdded(object sender, ControlEventArgs e)
{
Console.WriteLine("control added at run-time");
if(e.Control is FirstUserControl)
{
FirstUserControl RunTimeAddedFirstUserControl = e.Control as FirstUserControl;
RunTimeAddedFirstUserControl.FirstButton.Click += RunTimeAddedFirstButton_Click;
}
}
Summary: for simple scenarios there are ways to easily modify the visual attributes, state, or behavior of UserControls either: existing already in a container (drag-dropped at design-time), or, added at run-time to a container (dynamic).
This has the advantage of leaving the UserControl definition "simple and dumb," and not creating dependencies between specific Forms ("hosts") and specific facilities in the UserControl.
For more complex scenarios, involving Container Controls with more than one instance of the same UserControl at design-time, or having multiple instances of the same UserControl create at run-time: this may not be a best practice, and with complexly different behaviors and visual look associated with each different instance of the same UserControl: this strategy may require far too much complexity.
In the more complex scenario described above, it is probably best to analyze all the different UserControl requirements, and, if possible define a base UserControl class from which the differently behaving, and looking, other UaerControls inherit from and modify/over-ride.
best, Bill