Click here to Skip to main content
13,597,094 members
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 16 Oct 2009
Licenced CPOL

A Simple Way to Operate Multiple Controls at Runtime

, 16 Oct 2009
Rate this:
Please Sign up or sign in to vote.
This article presents a simple solution to operate multiple UI elements at runtime depending on one condition


I like to design convenient user interfaces. Usually it contains menus and toolbars, they provide many benefits. But still I found out eventually that if I want to check/uncheck or enable/disable some buttons or menu elements (for example: disable button and respective menu item that select data from database if we are not connected to one), Visual Studio ToolStripItem controls don't give me much space to do that. Basically that is what PropertyBinding stands for, but ToolStripItem doesn't have one.

So, that's the problem: we need a simple way to change a property of several UI elements by updating just one condition.


So let's see, what do we need to solve a problem stated earlier? Basically, that's a class that contains:

  • value of condition
  • collection of controls to operate
  • event, that will fire on value changing

Let's begin from the last statement:

public delegate void PropertyChangedHandler();

public class PropertyChangedEvent
    protected event PropertyChangedHandler handler;
    protected virtual void OnPropertyChanged()
        if (handler != null)

Pretty simple as it is, the code provides all we need. Keeping in mind that code must be as reusable as possible, I decided to separate this event and derive an abstract generic class from it. This abstract class will contain all basic functions that we need here.

public abstract class BindingProperty<t>: PropertyChangedEvent
    protected T defaultValue;
    protected T value;
    public T Value
        get { return this.value; }
        set { this.value = value; base.OnPropertyChanged(); }

    protected List<k> controls = new List<k>();
    public List<k> Controls
        get { return controls; }
    public void SetDefault()
        Value = defaultValue;
    public abstract void eventHandler();

Short Explanation

  • T is a type of condition value. In this article, I will use bool for example.
  • K is a type of control that implementation of this class will work with. I will use ToolStripItem.
  • defaultValue is the value that should be set during the initialization.
  • Value is the main property that will be used from the application. After changing it, the event from the parent class will fire.
  • Controls is a List of controls type.
  • Calling SetDefault will reset the condition value to its default.
  • And last, by implementing eventHandler we will get the work done. I decided to make it abstract, so developers will have to implement it and won't forget anything.

Using the Code

First of all, our generic abstract class must be implemented. It should look something like this (depends on your task, of course):

public class EnabledBindingProperty: BindingProperty<bool, />
    public EnabledBindingProperty(bool AValue)
        value = AValue;
        this.defaultValue = AValue;
        base.handler += new PropertyChangedHandler(eventHandler);
    public override void eventHandler()
        foreach (ToolStripItem c in controls)
        c.Enabled = value;

Any tampering with the controls must be placed in the eventHandler body. In this example, I change the Enabled property.

Next step. Declare a private variable of type EnabledBindingProperty and add a code like this to the constructor of the main form of your application:

private EnabledBindingProperty enabledProp = new EnabledBindingProperty(true);
public MainForm()
	//adding controls to enabledProp

So that's it, actually. The last thing that remains is to place the condition switcher somewhere. Like this:

private void Button1Click(object sender, EventArgs e)
	enabledProp.Value = true;

Points of Interest

Of course, these are just trivial samples. If you find this useful (and this is my first attempt to write an article at CodeProject), please keep me informed.


  • October 16, 2009 - Initial release posted


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


About the Author

Victor Lapin
Software Developer
Russian Federation Russian Federation
Has almost 10-years experience of programming on Delphi. Recently started to use Microsoft .NET Framework and C# language for daily job. Likes to develop convenient and user-friendly GUIs.

You may also be interested in...


Comments and Discussions

GeneralExcellent article, thanks ! Pin
BillWoodruff17-Oct-09 13:57
memberBillWoodruff17-Oct-09 13:57 
GeneralRe: Excellent article, thanks ! Pin
Victor Lapin17-Oct-09 21:03
memberVictor Lapin17-Oct-09 21:03 
GeneralRe: Excellent article, thanks ! Pin
Roberto Collina19-Oct-09 20:39
memberRoberto Collina19-Oct-09 20:39 
GeneralRe: Excellent article, thanks ! Pin
Victor Lapin20-Oct-09 1:01
memberVictor Lapin20-Oct-09 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180621.3 | Last Updated 16 Oct 2009
Article Copyright 2009 by Victor Lapin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid