在Visual Basic 2012中使用变量图像引用图片框

时间:2014-12-03 03:58:59

标签: vb.net visual-studio variables object visual-studio-2012

我正在尝试修改找到的答案here,但我没有太多运气......

我要做的是:

我有12张照片和12张照片。 在循环中我试图让它检查数字并加载相应的图像 所以,如果它是#3,它应该在图片框1-3中加载图像1-3

我认为我很接近,但我无法弄清楚

字典声明和传递子程序

Dim ctrlDict As New Dictionary(Of Image, Control)
Dictionary(ctrlDict)

定义子

 Sub dicti(pictures() As Image, ByRef ctrlDict As Dictionary(Of Image, Control))

    ctrlDict.Add(pictures(0), PictureBox1)
    ctrlDict.Add(pictures(1), PictureBox2)
    ctrlDict.Add(pictures(3), PictureBox3)

End Sub

循环

我得到的错误是“programname.my.resources是一个名称空间,不能用作表达式”而“picturebox是一种类型,不能用作表达式”

Sub Output(Days() As String, Prices() As String, WhichDay As String, total As Double, ctrlDict As Dictionary(Of Image, Control))

    For i As Integer = 0 To 11
        If WhichDay >= i Then
            ctrlDict(PictureBox & i).Image = pictures(i)
        End If
    Next
End Sub     

2 个答案:

答案 0 :(得分:0)

脱离我的头脑,很确定你无法做到

ctrlDict(PictureBox & i).Image = pictures(i)

你需要找到name属性等于(PictureBox.name& i)的控件。然后使用该控件并设置图像。

此计算机上没有IDE,但它会像

dim controlToUse as PictureBox
for each ctrl in ctrlDict
    if ctrl.value.name = "Whatever youre calling your pictureboxes" & i then
        controlToUse = ctrl.value
        exit for
    end if
next

if not controlToUse is nothing then controlToUse.image = pictures(i)

请注意,上面是伪造的。不要指望它编译!

答案 1 :(得分:0)

我找到了答案

Dim pic As PictureBox

    For i As Integer = 0 To 11
        If WhichDay >= i Then
            pic = Me.Controls("picturebox" & i + 1)
            pic.Image = pictures(i)
        End If
    Next