从图像列表中加载和查看图像

时间:2012-01-16 13:28:53

标签: vb.net

如您所见,下面给出的代码不是很有用。是否可以缩短代码。鼠标滚轮前后移动给出相同的结果(下一张图片)。无法配置Keydown。

Private Sub Images_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    count += 1
    If count + 1 > ImageList1.Images.Count Then
        count = 0
    End If
    PictureBox1.Image = ImageList1.Images.Item(count)
End Sub

Private Sub Images_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseWheel
    count += 1
    If count + 1 > ImageList1.Images.Count Then
        count = 0
    End If
    PictureBox1.Image = ImageList1.Images.Item(count)
End Sub

Private Sub Images_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.Down Then
        count += 1
        If count + 1 > ImageList1.Images.Count Then
            count = 0
        End If
        PictureBox1.Image = ImageList1.Images.Item(count)
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

这是一种缩短此功能的方法,只需使用功能:

Private Sub Images_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PictureBox1.Image = ImageList1.Images.Item(increaseCount(count))
End Sub

Private Sub Images_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseWheel
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
End Sub

Private Sub Images_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Down Then
   PictureBox1.Image = ImageList1.Images.Item(increaseCount(count))
End If
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 Sub

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 Sub

e.Delta是您滚动了多少,这取决于控制面板中有关鼠标滚轮滚动的选项。所以我们不能确定这些值是什么,但好处是滚动总是积极的,向下滚动是负面的。