简体   繁体   English

指定Picturebox VB.net

[英]Specify Picturebox VB.net

This is probably a very simple question, but I'm not sure how to phrase it. 这可能是一个非常简单的问题,但是我不确定该如何表达。 I'll just explain what I'm trying to do. 我只解释我要做什么。

I've made a grid of PictureBoxes, and want to make ones light up when activated. 我制作了PictureBoxes的网格,并希望在激活时使其点亮。 They range from picturebox1 to picturebox200, and I have an integer specifying which one I want lit. 它们的范围从picturebox1到picturebox200,我有一个整数,指定我要点亮的那个。 How would I go about (in pseudocode): 我将如何处理(以伪代码):

Dim P as integer = 50

me.pictureboxP.image = my.resources.on

The only way I know at the moment is a massive select case quagmire. 我目前所知道的唯一方法就是巨大的选择案例泥潭。

Add them to an array. 将它们添加到数组。 Then use the index to select which one you want. 然后使用索引选择所需的索引。

You can do this by using the Controls.Find method of the Form. 您可以通过使用Form的Controls.Find方法来执行此操作。 Something like the following should work: 类似于以下内容的东西应该起作用:

Dim p as Integer = 50
Dim pic as PictureBox = Me.Controls.Find("picturebox" + p.ToString, True)
If Not pic Is Nothing Then
    pic.Image = My.Resources.Icon
End If

See also this question: How do I refer to a windows form control by name (C# / VB) 另请参阅以下问题: 如何通过名称(C#/ VB)引用Windows窗体控件

Controls.Find() returns an ARRAY of Control, even if there is only one match. 即使只有一个匹配项,Controls.Find()也会返回一个控制数组。 Thus you need to check for any return values using the Length of the Array and then CAST the returned control to your desired type: 因此,您需要使用数组的长度检查是否有返回值,然后将返回的控件转换为所需的类型:

Dim p as Integer = 50
Dim matches() As Control = Me.Controls.Find("picturebox" + p.ToString, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is PictureBox Then
    Dim pic as PictureBox = DirectCast(matches(0), PictureBox)
    pic.Image = My.Resources.Icon
End If
  1. Add the picture boxes to an array 将图片框添加到数组
  2. Set tag property to True for 'ON' images and False for 'OFF' images 对于“ ON”图像,将标记属性设置为True,对于“ OFF”图像,将标记属性设置为False
  3. Then to select your ON pictures, just do the following and then highlight them as you wish 然后选择您要打开的图片,只需执行以下操作,然后根据需要突出显示它们即可

      MyArray.Select(function(x) x.Tag = True) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM