我有 2 个表单,frmMain 和 frmPictures。在 frmMain 中,我有一个计时器,其间隔为 5000(5 秒)。 frmPictures 有 16 个图片框,其中已加载图像。在每个计时器滴答声中,我需要更改 frmMain 背景图像。启动时,背景图像与 picturebox1 相同。
在每个计时器滴答声中,程序应随机选择 frmPictures 中的 PictureBox 并更改frmMain 的背景图像添加到所选 PictureBox 的图像中。
如何在 VB.NET 中执行此操作?
最佳答案
首先,您应该将所有PictureBox
收集到一个数组或类似结构中。这可能会发生,例如在 Form_Load
事件中:
Dim pictures(15) As PictureBox
pictures(0) = frmPictures.PictureBox1
'...
顺便说一句,为什么每张图片都有图片框?在应用程序启动时加载图像就足够了:
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
关于vb.net - 如何定期更改背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19195139/