vb.net 在 Visual Basic 2010 中动态循环图片框控件似乎不遵循任何顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14129652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically looping through picture box controls in Visual Basic 2010 does not seem to follow any order
提问by Dean
In a Visual Basic 2010 form application I have the below code snippet:
在 Visual Basic 2010 表单应用程序中,我有以下代码片段:
For Each ctlControl In Me.Panel1.Controls
If TypeName(ctlControl) = "PictureBox" Then
ctlControl.image = Nothing
End If
Next ctlControl
My problem is when it loops through the controls it does not start with the top left control and it seems to go over each picture box in random order.
我的问题是,当它遍历控件时,它不会从左上角控件开始,而且似乎以随机顺序遍历每个图片框。
How do I control the order of which picture box is updated next. Is there a property similar to tab index(in VB 6) which I can manipulate to control the order in which picture boxes are updated by my loop?
如何控制接下来更新哪个图片框的顺序。是否有类似于选项卡索引(在 VB 6 中)的属性,我可以操纵它来控制循环更新图片框的顺序?
采纳答案by Zeddy
As some of the other guys have said you could use the TAG property which is probably the BEST shot, whenyou are dynamically creating the picture boxes use a counter and add the counter value to the TAG property. if you added the picture boxes manually then simply start with the top left and work towards the right and add a value in the TAG property field of each one starting with 1 and increasing by one each time and continue until the row is completed then carry on with the next row.
正如其他一些人所说,您可以使用 TAG 属性,这可能是最好的镜头,当您动态创建图片框时,请使用计数器并将计数器值添加到 TAG 属性。如果您手动添加图片框,那么只需从左上角开始向右工作,然后在每个的 TAG 属性字段中添加一个值,从 1 开始,每次增加 1,然后继续直到该行完成,然后继续与下一行。
Finally when your ready to loop through the picture boxes simply follow the pattern below..
最后,当您准备好循环浏览图片框时,只需按照以下模式操作即可。
'Calc number of picture boxes
For Each ctlControl In Me.Panel1.Controls
If TypeName(ctlControl) = "PictureBox" Then
Counter = Counter + 1
End If
Next ctlControl
ThisBox = 1
Do
For Each ctlControl In Me.Panel1.Controls
If TypeName(ctlControl) = "PictureBox" Then
If CInt(ctlControl.Tag) = ThisBox Then
CLEAR-YOUR-IMAGE-HERE
ThisBox = ThisBox + 1
End If
End If
Next ctlControl
Loop Until ThisBox = Counter
Note: Its IMPORTANT your numbers that you place in the TAG property are consecutive or you will become forver stuck in the DO-LOOP!!!
注意:重要的是您放置在 TAG 属性中的数字是连续的,否则您将永远卡在 DO-LOOP 中!!!
回答by theGD
As a more proper and sure way, I would get each picture box, keep handles and locations of them, then sort them according to their location. Now they are ready to use. Here is an example:
作为一种更恰当和确定的方法,我会获取每个图片框,保留它们的手柄和位置,然后根据它们的位置对它们进行排序。现在它们可以使用了。下面是一个例子:
Public Class Form1
Structure Pbox
Dim handle As IntPtr
Dim top As Integer
Dim left As Integer
End Structure
Dim pboxlist As New List(Of Pbox)
Sub ClearImages()
pboxlist.Clear()
For Each c As Control In Me.Controls
If TypeName(c) = "PictureBox" Then
Dim x As New Pbox
x.top = c.Top
x.left = c.Left
x.handle = c.Handle
End If
Next
pboxlist.OrderByDescending(Function(a) a.top).ThenByDescending(Function(a) a.left)
For Each item In pboxlist
Dim x As PictureBox = PictureBox.FromHandle(item.handle)
x.Image = Nothing
Next
End Sub
End Class
Another approach is using a good naming, so that you can use their names to sort them. For instance, PictureBox1 will come before PictureBox2 if you sort. So you should use PictureBox1 for the very top and left one and PictureBox2 for the next one and so on...
另一种方法是使用一个好的命名,这样你就可以用它们的名字来对它们进行排序。例如,如果排序,PictureBox1 将排在 PictureBox2 之前。所以你应该在最顶部使用 PictureBox1,在下一个使用 PictureBox2,依此类推...
EDIT:Using Tag property, as John Bustos suggested, instead of names is an easier and better idea. So without getting lost in names, you can sort picture boxes according to their Tags which are defined by you.
编辑:正如 John Bustos 所建议的那样,使用 Tag 属性而不是名称是一个更容易和更好的主意。因此,不会迷失在名称中,您可以根据自己定义的标签对图片框进行排序。
回答by ourmandave
The PictureBox control has a Text property you can use instead of Tag.
PictureBox 控件有一个 Text 属性,您可以使用它来代替 Tag。
It doesn't come up in Intellisense because it's an infrastructure property, but it's there.
它不会出现在 Intellisense 中,因为它是基础设施属性,但它就在那里。
http://msdn.microsoft.com/en-us/library/hc9k45f4(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/hc9k45f4(v=vs.110).aspx
(I wanted to comment on Zaf Khan's answer but I don't have the rep, yet.)
(我想对 Zaf Khan 的回答发表评论,但我还没有代表。)
回答by U1199880
The order of the controls was determined by the order they were added to the panel and not tabstop index. You can change that by carefully reorganizing the order they were added to the panel in the form's designer file, though I'd not recommend it.
控件的顺序由它们添加到面板的顺序决定,而不是制表位索引。您可以通过仔细重新组织它们在表单设计器文件中添加到面板的顺序来更改它,尽管我不建议这样做。

