使用循环调用图像
嘿,我需要一些有关 C# Windows 窗体应用程序作业的帮助。假设我有一个由 10 个图片框组成的数组,它们的名称为“pic0”、“pic1”等。我需要在代码中用 for 循环调用它,这就是我的问题。如果我解释得不好,我很抱歉,但我会尝试向您展示我尝试过的代码。
for(int i=0;i<Array.Length;i++)
{
("pic" + i).Visible = true;
}
我在循环中尝试做的是循环遍历数组,使循环中的所有内容都可见。 问题是我不认为调用 ("pic" + i).Visible 有效。有什么建议吗?
Hey I need some help with C# Windows Form Application homework. Suppose I had an array of 10 pictureboxes and they had the names "pic0", "pic1" and so on. I need to call it in the code with a for loop, and that is my problem. Im sorry if I explained this badly, but I will try and show you the code that I tried.
for(int i=0;i<Array.Length;i++)
{
("pic" + i).Visible = true;
}
What I am trying to do in the loop is to loop through the array, making everything visible with the loop.
The problem is that I dont think calling ("pic" + i).Visible works. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用数组而不是名称 - 数组中的每个元素都将是一个 Picturebox,您可以在其中设置
Visible
属性。Use the array instead of the name - each element in the array will be a Picturebox on which you can set the
Visible
property.好吧,如果您有一个图片框数组,您可以迭代该数组的元素(可能是 PictureBox 元素的数组,而不是其名称的数组)并设置每个元素的 Visible 属性。
迭代 PictureBox 元素数组的另一种方法是使用如下的 foreach 语句:
但是,如果您只有(字符串)数组中的名称,那么您将需要以不同的方式执行操作。
Well, if you had an array of picture boxes you could iterate through the elements of the array (which is presumably an array of PictureBox elements, not of their names) and set each element's Visible property.
Another way to iterate over an array of PictureBox elements is to use a foreach statement like this:
However, if you only have the names in an array (of strings) then you will need to do things differently.
表达式
("pic" + i)
可能甚至无法编译,如果编译,它会返回一个string
类型的对象。string
类型的对象没有Visible
属性。您需要一种方法来查找所需的 Windows 窗体控件。实际上,数组中已经拥有所有这些对象。
The expression
("pic" + i)
probably doesn't even compile, and if it does, it returns an object of typestring
.Objects of type
string
do not have aVisible
property.You need a way of finding the Windows Forms Control that you want. Actually, you already have all those objects in the array.