Click here to Skip to main content
12,999,633 members (44,667 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Say i create a user control.
this control needs to behave diffrently when invoked through different sources.
how do i handle that?
Posted 14-Sep-12 1:11am
ryanb31 14-Sep-12 8:08am
You include properties on it that the different sources can set so that it behaves differently.
Sergey Alexandrovich Kryukov 14-Sep-12 13:16pm
This is most reasonable approach. Why a control should detect anything about the environment? -- it's just a control.
Sergey Alexandrovich Kryukov 14-Sep-12 13:15pm
Not enough information to be a real question. There is nothing to answer about until you provide further detail and explain what do you want to achieve. Don't forget to explain the goal of it.
Sergey Alexandrovich Kryukov 14-Sep-12 13:17pm
Besides, you need to tag your application type or the UI library you want to use (WPF? Forms? ASP.NET? Silverlight? Metro? what?)
Roopica 18-Sep-12 2:16am
Say I develop a control, it is once invoked from say one source, it needs some sort of behavior and when invoked from another source , it needs to behave from other sort.How do i program my control accordingly?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:
// declare a variable of Type UserControl
FirstUserControl frmUC;
private void Form1_Load(object sender, EventArgs e)
    // does the UserControl exist on this Form now ?
    // note the requirement to use a string here
    frmUC = this.Controls["firstUserControl1"];
    if(frmUC != null)
        // UserControl present so we convert it
        // to the specific type of our UserControl
        FirstUserControl firstUC = frmUC as FirstUserControl;
        // get the button
        Button theButton = firstUC.FirstButton
        // assign an event handler to it
        // EventHandler code not shown here
        theButton .Click += FirstButton_Click;
        // set a visual property of the UserControl
        firstUC.BackColor = Color.WhiteSmoke;
        // set visual properties of the Button
        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");
     // have we just added an instance of our specific UserControl ?
     if(e.Control is FirstUserControl)
         FirstUserControl RunTimeAddedFirstUserControl = e.Control as FirstUserControl;
        // and now add can an event handler to it
         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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 17 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100