使用按钮在一个图片框中更改两个图像(VB.NET) [英] Change two images in one picture box using a button (VB.NET)

查看:330
本文介绍了使用按钮在一个图片框中更改两个图像(VB.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试更改图片框中的图像。它可以工作,如果我想用一个图像更改它,但我无法让它更改为另一个图像。当我点击按钮时,它应该在两个图像之间交替。

I've been trying to change the image in a picture box. It works if I want to change it with one image, but I can't get it to change to the other image. It should alternate between the two images when I click the button.

这是我的代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Button1.Click

    Dim num As Boolean

    If num = False Then

        PictureBox3.Image = My.Resources.Beep
        num = True

    Else

        PictureBox3.Image = My.Resources.Skateboard
        num = False

    End If


End Sub

我一直想弄清楚为什么它不能长时间工作,任何帮助都会受到赞赏。

I've been trying to figure out why it doesn't work for a long time, any help wouldbe appreciated.

推荐答案

您的变量 num 是方法的本地,因此您可以按照自己的喜好进行更改,但每次调用此代码时重新创建变量 num 并给出默认的初始值False。

从它的位置开始它将为True你将它设置为方法的退出

You variable num is local to the method, so you could change it how you like, but everytime this code is called the variable num is recreated and given the default initial value of False.
It will be True just from the point in which you set it to the exit of the method

要解决问题,你需要声明它共享或在全球级别的程序之外宣布

To resolve the problem you need to declare it Shared or declare it outside the procedure at class global level

共享选项

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Button1.Click

    Dim Shared num As Boolean
    If num = False Then
        PictureBox3.Image = My.Resources.Beep
        num = True
    Else
        PictureBox3.Image = My.Resources.Skateboard
        num = False
    End If
End Sub

班级选项

Dim num As Boolean

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Button1.Click
.....
End Sub

这篇关于使用按钮在一个图片框中更改两个图像(VB.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆