无法转换类型为System.Windows.Forms.Button'的对象。键入' System.Windows.Forms.PictureBox'

时间:2012-06-06 16:28:01

标签: vb.net-2010

我正在尝试对多个图片框执行相同的操作,但以下代码的第一行产生了此错误:

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 个答案:

答案 0 :(得分: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