Click here to Skip to main content
Click here to Skip to main content

A Simple Way to Operate Multiple Controls at Runtime

, 16 Oct 2009 CPOL
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

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<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()
{
	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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

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.

Comments and Discussions

 
GeneralExcellent article, thanks ! PinmemberBillWoodruff17-Oct-09 14:57 
5#
 
Hi Victor,
 
I am going to learn a lot studying your code !
 
best, Bill
 
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844

GeneralRe: Excellent article, thanks ! PinmemberVictor Lapin17-Oct-09 22:03 
GeneralRe: Excellent article, thanks ! PinmemberRoberto Collina19-Oct-09 21:39 
GeneralRe: Excellent article, thanks ! PinmemberVictor Lapin20-Oct-09 2:01 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 16 Oct 2009
Article Copyright 2009 by Victor Lapin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid