Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Simple Way to Operate Multiple Controls at Runtime

0.00/5 (No votes)
16 Oct 2009 1  
This article presents a simple solution to operate multiple UI elements at runtime depending on one condition

Introduction

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.

Solution

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)
            handler();
    }
}

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
{
    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()
{
	InitializeComponent();
			
	//adding controls to enabledProp
	enabledProp.Controls.Add(toolStripButton1);
	enabledProp.Controls.Add(toolStripButton2);
	enabledProp.Controls.Add(toolStripSplitButton1);
	enabledProp.Controls.Add(subitem1ToolStripMenuItem);
	enabledProp.Controls.Add(subitem2ToolStripMenuItem);
	enabledProp.SetDefault();
}

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.

History

  • October 16, 2009 - Initial release posted

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here