【发布时间】:2019-05-29 12:30:49
【问题描述】:
所以我有一个按钮矩阵,从 a1f 到 a10f,从 a 到 j,所以 a1f 在左上角,j10 在右下角。
我想做这样的事情:
for (i = 1; i < 11; i++)
{
a{i}f.BackgroundImage = Properties.Resources._1mal2_1_Rebellion;
b{i}f.BackgroundImage = Properties.Resources._1mal2_2_Rebellion;
a{i}f.Enabled = false;
a{i}f.Tag = "playerShip";
b{i}f.Enabled = false;
b{i}f.Tag = "playerShip";
}
所以第一个循环是:
a1f.BackgroundImage = Properties.Resources._1mal2_1_Rebellion;
b1f.BackgroundImage = Properties.Resources._1mal2_2_Rebellion;
a1f.Enabled = false;
a1f.Tag = "playerShip";
b1f.Enabled = false;
b1f.Tag = "playerShip";
第二个是:
a2f.BackgroundImage = Properties.Resources._1mal2_1_Rebellion;
b2f.BackgroundImage = Properties.Resources._1mal2_2_Rebellion;
a2f.Enabled = false;
a2f.Tag = "playerShip";
b2f.Enabled = false;
b2f.Tag = "playerShip";
等等..
a{i}f 或 a[i]f 不起作用。
【问题讨论】:
-
你不能那样做一个变量引用。将按钮引用存储在数组中可能会更好,这样您就可以对其进行索引以获取引用。
-
创建一个
Buttons 数组并通过for (i = 1; i < 11; i++) { af[i].访问它