Recursively notify child controls via C#

前端 未结 4 2003
执笔经年
执笔经年 2020-12-18 08:56

I have a form MainForm which is a Windows Forms form that contains many child controls. I want to call one function on MainForm that notifies all o

4条回答
  •  误落风尘
    2020-12-18 09:38

    The answer from MusiGenesis is elegant, (typical in a good way), nice and clean.

    But just to offer an alternative using lambda expressions and an 'Action' for a different type of recursion:

    Action traverse = null;
    
    //in a function:
    traverse = (ctrl) =>
        {
             ctrl.Enabled = false; //or whatever action you're performing
             traverse = (ctrl2) => ctrl.Controls.GetEnumerator();
        };
    
    //kick off the recursion:
    traverse(rootControl);
    

提交回复
热议问题