프로그램 자료/Visual C#

폼에 있는 모든 콘트롤 가져오기

motolies 2016. 7. 12. 10:15

출처 : http://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-windows-forms-form-of-a-specific-type-button#3426721



함수

public IEnumerable<Control> GetAll(Control control, Type type)

{

    var controls = control.Controls.Cast<Control>();

 

    return controls.SelectMany(ctrl => GetAll(ctrl, type))

                                .Concat(controls)

                                .Where(c => c.GetType() == type);

}

 

사용법

GetAll(this, typeof(Label))