Click here to Skip to main content
15,885,985 members
Articles / Desktop Programming / Windows Forms

A Simple Way to Operate Multiple Controls at Runtime

Rate me:
Please Sign up or sign in to vote.
4.75/5 (11 votes)
16 Oct 2009CPOL2 min read 24K   218   24   4
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:

C#
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.

C#
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):

C#
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:

C#
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:

C#
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)


Written By
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 ! Pin
BillWoodruff17-Oct-09 13:57
professionalBillWoodruff17-Oct-09 13:57 
GeneralRe: Excellent article, thanks ! Pin
Victor Lapin17-Oct-09 21:03
Victor Lapin17-Oct-09 21:03 
GeneralRe: Excellent article, thanks ! Pin
rcollina19-Oct-09 20:39
rcollina19-Oct-09 20:39 
GeneralRe: Excellent article, thanks ! Pin
Victor Lapin20-Oct-09 1:01
Victor 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.