如何使用其名称作为for循环中的字符串来访问按钮

Kri*_*ota 0 c# winforms

我需要访问for循环中的按钮,但它的名称必须更改.

例如:

  • 有许多按钮的名称是bt1,bt2,bt3,... bt25.
  • 我在我的代码中使用for循环以达到某种目的,以禁用或启用这些按钮.
  • 我可以使用for循环来做到这一点吗?

喜欢:

for(int i =1;i<25;i++)
{
    "bt"+"i".Enable = True;
}
Run Code Online (Sandbox Code Playgroud)

我如何将字符串作为控件?

Moh*_*hin 7

for(int i =1;i<25;i++)
{
    this.Controls["bt"+ i.ToString()].Enable = True;
}
Run Code Online (Sandbox Code Playgroud)

VB(使用代码转换器):

For i As Integer = 1 To 24
    Me.Controls("bt" & i.ToString()).Enable = [True]
Next
Run Code Online (Sandbox Code Playgroud)