gpt4 book ai didi

c# - 标签数组不工作 c#

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:54 27 4
gpt4 key购买 nike

我想使用标签数组,但它不起作用我不知道我错过了什么。这是我试图开始工作的代码:

for (int x = 1; x <= 10; x++)
{
Label[] label1 = new Label[10];
label1[x] = new Label();
label1[x].AutoSize = true;
label1[x].Text = "text";
label1[x].Left +=10;
label1[x].Top +=10;
}

最佳答案

您将在每次迭代中初始化一个新的 Label1 数组,因此您最终将只有最后一个在最后一个位置有 1 个项目。

label1 的声明移到循环之外:

//Move this line outside of the loop's scope
Label[] label1 = new Label[10];

//Loop from 0 to the Length of the array instead of repeating 10 again
for (int x = 0; x < label1.Lenth; x++)
{
label1[x] = new Label();
label1[x].AutoSize = true;
label1[x].Text = "text";
label1[x].Left +=10;
label1[x].Top +=10;
}

我建议您查看 MSDN关于使用数组:

关于c# - 标签数组不工作 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39899815/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com