如何定期更改背景图像

时间:2013-10-05 07:19:28

标签: vb.net timer

我有两种形式,frmMain和frmPictures。在frmMain中,我有一个间隔为5000(5秒)的计时器。 frmPictures有16个图片框,其中已经加载了图像。在每个计时器刻度我需要更改frmMain背景图像..启动时背景图像与picturebox1相同。

在每个计时器刻度上,程序应随机选择frmPictures中的PictureBox并更改frmMain的背景图像到所选PictureBox的图像。

如何在VB.NET中执行此操作?

1 个答案:

答案 0 :(得分:2)

首先,您应该在数组或类似结构中收集所有PictureBoxes。这可能发生在例如在Form_Load事件中:

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
相关问题