从数组创建标签

编程入门 行业动态 更新时间:2024-10-25 18:24:18
本文介绍了从数组创建标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想基于数组创建标签,但是我总是只得到一个标签.

I want to create labels based on an array, but i always get only one label.

private void button1_Click(object sender, EventArgs e) { Debug.WriteLine(hardrive.GetHardDriveName.Count); Label[] lblHDDName = new Label[hardrive.GetHardDriveName.Count]; for (int i = 0; i < hardrive.GetHardDriveName.Count; i++) { int x = 10; int y = 10; lblHDDName[i] = new Label(); lblHDDName[i].Location = new System.Drawing.Point(x, y); lblHDDName[i].Text = "Test"; groupBoxHDD.Controls.Add(lblHDDName[i]); y += 10; } }

调试

Debug.WriteLine(hardrive.GetHardDriveName.Count);

在数组中显示两个项目.

Shows two items in the array.

问题在于,GroupBox中只有一个标签,而不是两个.

The problem is that in the GroupBox there is only one label instead of two.

推荐答案

您的y变量是在for循环中定义的,而不是在外部定义的.因此,对于循环的每次执行,都将其初始化为10并在System.Drawing.Point中使用它.如果要跟踪循环结束时完成的增量,则必须在for循环之前声明并初始化y.

Your y variable is defined in the for loop, not outside. Therefore, for each execution of the loop, you initialize it to 10 and use it in your System.Drawing.Point. If you want to keep track of the increment done at the end of the loop, you must declare and initialize y before the for loop.

int y = 10; for (int i = 0; i < ...; i++) { // use y ... // increment it y += 10; }

更多推荐

从数组创建标签

本文发布于:2023-10-19 18:12:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   标签

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!