vb.net - 我正在尝试将图片框添加到数组中,以便稍后在以编程方式创建它们时引用它们
问题描述
到目前为止,我只有这段代码......
Dim lineb As New PictureBox
lineb.Size = New Size(24, 24)
lineb.BackColor = Color.FromArgb(0, 192, 192)
lineb.Location = New Drawing.Point(xb, yb)
Controls.Add(lineb)
lineb.Name = "lineb" + CStr(creatorb)
creatorb += 1
这会在 timer.tick 事件中生成无限行的图片框。Xb 和 Yb 连续移动,这很有效。我需要弄清楚如何将每个图片框添加到数组或稍后引用它们的另一种方式。每个都被创建并重命名为 lineb + 1...2...3...4... 等。
解决方案
下面是一个跟踪动态创建的控件的方法示例。这比将每个控件添加到数组更容易。
Dim pbox as New PictureBox
With pbox
.location = New Point(xb, yb)
.size = New Size(24, 24)
.Tag = "box1" 'choose a tag that will help identify it later
' and so on
Addhandler pbox.click, AddressOf pbox_click '' to add a click event handler
End With
Me.Controls.add(pbox)
Private Sub pbox_click(sender As Object, e As EventArgs)
Dim the_sender As PictureBox = DirectCast(sender, PictureBox)
Dim reference As String = DirectCast(the_sender.tag, String)
''' do whatever you need to do now that you know which picturebox was clicked.
End Sub
或者,您可以随时循环浏览控件以找到您要查找的控件:
For Each Ctrl As Control In Me.Controls
If TypeOf Ctrl Is PictureBox Then
If ctrl.tag = "box1" Then '''or whetever you are looking for
''' do your stuff here
End If
End If
Next
推荐阅读
- c++ - 以下 c++ 代码有什么问题?
- python - 如何在图像库中找到相同/重复的图像
- python - 无法为非均匀离散变量采用逆 cdf
- c - Which gotoxy() function should I use?
- javascript - 嵌入 Twitch Player 正在小提琴中工作,但不在本地 html 文件中
- php - how to grab each value from input with jquery ajax when inputs are in a foreach loop
- c# - ML Agents - Multiple agents break the training
- javascript - 打破承诺链和异步等待
- java - 将 MySQL 连接器 Jar 链接到我的依赖项 Java Play Framework 1.x
- python - Converting user input to time and substracting it from main time