2012-06-06 63 views
-1

我試圖對多個圖片框執行相同的操作,但下面的代碼的第一行正在生成這個錯誤:無法強制類型'System.Windows.Forms.Button'類型的對象類型'System.Windows.Forms.PictureBox'

Unable to cast object of type System.Windows.Forms.Button to type System.Windows.Forms.PictureBox 

所以我想知道是否有人可以幫助。提前致謝。

For Each pb As PictureBox In Me.Controls 
    Dim bp As New Bitmap(pb.Image) 
    pb.Region = GetRegion(bp, Color.FromArgb(255, 255, 0, 255)) 
    pb.Image = Nothing 
    pb.BackColor = Color.FromArgb(100, Color.Yellow) 
Next 

回答

1

For Each是遍歷所有的控件在Me.Controls

您需要檢查的PictureBox類型開始在控制操作之前

For Each ctrl As Control In Me.Controls 
    If TypeOf ctrl Is PictureBox Then 
     Dim bp As New Bitmap(ctrl.Image) 
     ctrl.Region = GetRegion(bp, Color.FromArgb(255, 255, 0, 255)) 
     ctrl.Image = Nothing 
     ctrl.BackColor = Color.FromArgb(100, Color.Yellow) 
    End If 
Next 
相關問題