C# WinForm: Accessing a certain Control in Controls

时间:2017-08-05 11:40:04

标签: c# winforms loops controls panel

I have 3 Controls inside a TabPage tabpage1; let's call them panel1, panel2 and datagridview1. I'm trying to make a general method for accessing panel2. How do I access this Panel in TabPage.Controls? I've found out that I can use something like tabpage1.Controls[1]. But how do I know the index of panel2? And how do I set its index?

1 个答案:

答案 0 :(得分:0)

I believe you can use the controls collection.

        var ctrls = this.Controls.Find("ControlName", true);
        if (ctrls != null)
           if(ctrls.Length != 0)
               {
                    Control ctrl = ctrls[0];
               }

I am not good in C# but I believe it works and you can easy make an universal method from it. (this only for the ilustrational purposes - replace with the apriopriate object.