Get control by name, including children

后端 未结 3 1949
-上瘾入骨i
-上瘾入骨i 2021-01-20 03:37

I\'m trying to get a control by name. I wrote the following code:

public Control GetControlByName(string name)
{
    Control currentControl; 

    for(int i          


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-20 04:16

    Just use the Controls collection Find method:

                var aoControls = this.Controls.Find("MyControlName", true);
                if ((aoControls != null) && (aoControls.Length != 0))
                {
                    Control foundControl = aoControls[0];
                }
    

提交回复
热议问题