使用鼠标滚轮和向上按键在图像列表中导航图像

时间:2012-01-17 10:21:15

标签: vb.net

下面给出的代码工作正常。唯一的问题是显示TOP图像后无法使用鼠标滚轮/键盘,它填充键盘缓冲区。然后我必须等待一段时间才能使用wheel / keydown。此外,车轮/ keydown之后的图像超出了最后一张图像。它应该停在最后一个图像,就像它停在第一个图像。代码由Geek On Demand提供。

Private Sub Images_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理MyBase.Load         PictureBox1.Image = ImageList1.Images.Item(increaseCount(count))     结束子

Private Sub Images_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseWheel
    Try
        If e.Delta > 0 Then
            PictureBox1.Image = ImageList1.Images.Item(decreaseCount(count))
        ElseIf e.Delta < 0 Then
            PictureBox1.Image = ImageList1.Images.Item(increaseCount(count))
        End If
    Catch ex As Exception
    End Try
End Sub

Private Sub Images_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    Try
        If e.KeyCode = Keys.Down Then
            PictureBox1.Image = ImageList1.Images.Item(increaseCount(count))
        End If
    Catch ex As Exception
    End Try
End Sub

Private Sub Images_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
    Try
        If e.KeyCode = Keys.Up Then
            PictureBox1.Image = ImageList1.Images.Item(decreaseCount(count))
        End If
    Catch ex As Exception
    End Try
End Sub

Private Function increaseCount(ByRef count As Integer) As Integer
    count += 1
    If count + 1 > ImageList1.Images.Count Then
        count = 0
    End If
    Return count
End Function

Private Function decreaseCount(ByRef count As Integer) As Integer
    count -= 1
    If count - 1 > ImageList1.Images.Count Then
        count = 0
    End If
    Return count
End Function

1 个答案:

答案 0 :(得分:0)

您可以将减少计数功能修改为:

Private Function decreaseCount(ByRef count As Integer) As Integer
 count -= 1
  If count - 1 > ImageList1.Images.Count Or count<0 Then
    count = 0
  End If
 Return count
End Function