tags:

views:

346

answers:

2

Hi all,

I'm trying to do something I've been historically available to do using Visual FoxPro (VFP). By use of a "SETALL()" function, I am able to do something like pertaining to a current form... this.SetAll( "someProperty", "toSomeNewValue" ) and it goes through and assigns the value to all controls. By creating my own custom property and an internal to VFP via "_assign", will cause these events to trigger. From THAT method, each control would take care of itself on the form, from enable/disable/visibility, editable, readonly, color, font, validation, etc, based on what I put in each individual control.

So, how would I do a similar thing in C#... Such as have a public property, or method, or delegate at the form level, such as "FormEditMode". Then, when I do something like this.FormEditMode = AddMode, all controls will be self-triggered to turn themselves on/off, enable/disable respectively instead of explicit calls for all controls on a given form.

Thanks

+1  A: 

I would just do something like this:

this.Visible = false;
Controls.ForEach<Control>(c => c.Visible = false);

If you have GroupBoxes or other Controls that have Controls, you may want to put that into a recursive function:

static void DoForEachControl(Control control, Action<Control> f)
{
  control.Controls.ForEach<Control>(c =>
                                      {
                                        f(c);
                                        DoForEachControl(c, f);
                                      });
}

To get that ForEach you'll need something like this:

  public static class Extensions
  {
    public static void ForEach<T>(this IEnumerable source, Action<T> action)
    {
      foreach (var item in source)
      {
        action((T)item);
      }
    }
  }
RossFabricant
A: 

I actually got it solved by getting each control self-registered at the end of the form's InitialzeComponents() call, and a combination of other pieces I've read in my research for a solution.

After building my baseline form, textbox, label controls (and others), after the InitializeComponents(), I do a recursive call for each control on the form and register its method to the form's delegate/event. Then, during a call to change the form's Edit Mode, it invokes the delegate call and triggers each individuals controls function. So, I'm getting a better handle on the delegate / event handings, and now the recursive call. This is done ONCE up front, so I don't have to keep recursively checking and calling during a button's click, or some other condition to cycle through controls each time I need to do something.

DRapp