views:

50

answers:

3

So I have a main form with 3 controls whose Enable property I want to control using an enum.

All these controls have a reference to the Data that contains the Enum value Level.

enum Level
{
    Red,
    Yellow,
    Green
}

So if it's Red, I want the RedControl to become enabled, if it's yellow, then YellowControl becomes enabled, etc.

How do I best do this with minimal code and elegance?

I tried having 3 properties like IsRed, IsYellow, etc on the Data to hook them up. But then I didn't know a way to detect the change of Level from those properties.

A: 

RedControl.Enabled = ((value & Level.Red) != 0)

Ray
Reed Copsey
thanx reed - bad syntax - I corrected my post
Ray
A: 

Im not sure about databinding it... but what about putting enabling code in the property' s set?

ie

public YourClass
{
   Level _level;
   public Level level 
   {
      get{ return _level;}
      set 
      {
        _level = value;
        if(_level == Level.Green) { greenControl.Enable = true; //plus disable others }
        if(_level == Level.Yellow) { yellowControl.Enable = true; //plus disable others }
        if(_level == Level.Red) { redControl.Enable = true; //plus disable others }
      }
   }
}

that way your property works like normal (and I guess you can databind it but im not really sure) and when it gets changed the controllers will change.

Francisco Noriega
Thanks but all these control instances contain a reference to Data, and any change in the Data should alert all of them appropriately.
Joan Venge
Then what about rising an event when the data changes, and then in the eventhandler of each control have the control disable itself if it is not the data he likes, and enable itself it is the it needs.
Francisco Noriega
A: 

Your binding source class could implement the System.ComponentModel.INotifyPropertyChanged. I think it's a flexible way to do databinding in windows forms.

Here's one article on codeproject showing how to do it. I've haven't analysed it very deeply, though.

jpbochi