2014-12-03 48 views
0

我試圖修改答案發現here使用可變圖像,但我運氣不好....引用一個PictureBox在Visual Basic 2012

我試圖做的是:

我有12張照片和12個pictureboxes。 在循環中我試圖讓它檢查的數量和加載相應的圖像(S) 所以如果是#3應該載入圖像1-3 pictureboxes 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  

回答

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 
0

關閉我的頭頂,很肯定你不能這樣做

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

youll需要找到具有name屬性等於控制(PictureBox.name &我)。然後使用該控件並設置圖像。

不要在此計算機上的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) 

請注意,上面是僞代碼。不要指望它編譯!