2012-01-16 41 views
2

正如您所看到的,下面給出的代碼不是很有用。是否有可能縮短代碼。鼠標滾輪向前和向後給出相同的結果(下圖)。 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

這裏是縮短這種方式,只是使用功能:

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是你有多少滾動,這取決於對鼠標滾輪滾動的控制面板中的選項。所以我們不能確定這些值是什麼,但好事是滾動總是正面的,向下滾動是負面的。

+0

我的代碼看起來與上面的代碼整齊。有沒有鼠標滾輪(FORWARD)和鍵盤的方法。我已經嘗試過,但圖像的滾動方向與鼠標滾輪(BACKWARD)和keydown相同。 – Farook

+0

檢查編輯的代碼@查看這是根據您的需要。 – Harsh

+0

鼠標滾輪滾動工作正常。就像你提到的那樣,情況就是輪子滾動了多少。目前它必須緩慢滾動。高超。當你在第一張圖片時,停止滾動。我添加了一個Try Catch,引發了一個錯誤。 Keyup無法正常工作。 Keydown正在工作。我可能不會發表評論,因爲我可能會在一段時間後關閉。謝謝。一旦完成,我將點擊回答。 – Farook