鼠标悬停在VB上的图片并在点击时保持不变

时间:2015-11-15 09:52:31

标签: vb.net

所以我制作了一个这样的表格

Form1

当表单加载时,图片框将显示neutral.png,所以

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PictureBox1.Image = Image.FromFile("Images\neutral.png")
End Sub

我将mousebox上的图片更改为x.png并在鼠标离开时消失,所以

Private Sub PictureBox1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
    PictureBox1.Image = Image.FromFile("Images\x.png")
End Sub

Private Sub PictureBox1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
    PictureBox1.Image = Image.FromFile("Images\neutral.png")
End Sub

我的问题是当我点击picturebox1时,如何让x.png图像保留在picturebox1上。这样做

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    PictureBox1.Image = Image.FromFile("Images\x.png")
End Sub

似乎没有用,因为mouseleave事件仍然有效。

1 个答案:

答案 0 :(得分:2)

有一个例子,来自我的评论

Public Class Form1

    Private isClicked As Boolean = False

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        PictureBox1.Image = Image.FromFile("images\neutral.jpg")
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click
        If isClicked = False Then isClicked = True Else isClicked = False 'if You click again, everything back
        'isClicked = True 'in this case, when You click "x.jpg" will stay always
        PictureBox1.Image = Image.FromFile("images\x.jpg")
    End Sub

    Private Sub PictureBox1_MouseEnter(sender As Object, e As System.EventArgs) Handles PictureBox1.MouseEnter
        If isClicked = False Then 'picturebox isn't clicked, so show x.jpg ... otherwise x.jpg will be showed always
            PictureBox1.Image = Image.FromFile("images\x.jpg")
        End If
    End Sub

    Private Sub PictureBox1_MouseLeave(sender As Object, e As System.EventArgs) Handles PictureBox1.MouseLeave
        If isClicked = False Then 'picturebox isn't clicked, so show neutral image ... otherwise x.jpg will be showed always
            PictureBox1.Image = Image.FromFile("images\neutral.jpg")
        End If
    End Sub
End Class

使用某个变量,在此示例中为isClicked。 始终在MouseEnter下进行检查,MouseLeave点击picturebox

通过此示例,您可以再次点击picturebox,然后重置isClicked,这样您就可以再次neutralx图像。