2013-10-05 106 views
2

我有2種形式,frmMain和frmPictures。在frmMain我有一個定時器,其間隔爲5000(5秒)。 frmPictures有16個圖片箱,圖片已經加載在其中。在每個計時器刻度中,我需要更改frmMain背景圖像。在啓動時,背景圖像與picturebox1相同。

在每個計時器刻度上,程序應隨機選擇frmPictures中的PictureBox並將frmMain的背景圖像更改爲所選PictureBox的圖像。

如何在VB.NET中執行此操作?如何定期更改背景圖像

回答

2

首先,你應該收集所有PictureBoxes在一個數組或類似的結構。這可能發生,例如在事件:

Dim pictures(15) As PictureBox 
pictures(0) = frmPictures.PictureBox1 
'... 

順便說一下,爲什麼你還要爲每個畫面PictureBoxes?這將足以加載應用程序啓動時的圖片:

Dim pictures(15) As Image 
pictures(0) = Image.FromFile("...") 
'... 

然後在計時器事件,創建一個隨機數,並選擇一個圖片:

'Call Randomize() on application startup 
Dim rnd = CInt(16 * Rnd()) 
BackgroundImage = pictures(rnd).Image 'For the picture box method or 
BackgroundImage = pictures(rnd)  'For the direct method 
+0

好吧,這可能是更好的方式..謝謝:) – Rahul