有没有办法缩短这些 PictureBox Subs?
Is there a way to shorten these PictureBox Subs?
我的表单上有 9 个图片框。每个 PictureBox 都有自己的 DoubleClick 事件处理程序。我还没有找到将这 9 个 subs 合并为一个的方法;特别是因为位图列表 (List(Of System.Drawing.Bitmap)) 必须具有不同数量的 .count
。
如果单击 PictureBox,则只应执行 this 的代码。
Private Sub PictureBox1_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox1.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 0 Then
BitmapList.RemoveAt(0)
Path_List.RemoveAt(0)
PictureBox1.Image = Nothing
End If
End Sub
Private Sub PictureBox2_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox2.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 1 Then
BitmapList.RemoveAt(1)
Path_List.RemoveAt(1)
PictureBox2.Image = Nothing
End If
End Sub
Private Sub PictureBox3_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox3.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 2 Then
BitmapList.RemoveAt(2)
Path_List.RemoveAt(2)
PictureBox3.Image = Nothing
End If
End Sub
Private Sub PictureBox4_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox4.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 3 Then
BitmapList.RemoveAt(3)
Path_List.RemoveAt(3)
PictureBox4.Image = Nothing
End If
End Sub
Private Sub PictureBox5_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox5.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 4 Then
BitmapList.RemoveAt(4)
Path_List.RemoveAt(4)
PictureBox5.Image = Nothing
End If
End Sub
Private Sub PictureBox6_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox6.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 5 Then
BitmapList.RemoveAt(5)
Path_List.RemoveAt(5)
PictureBox6.Image = Nothing
End If
End Sub
Private Sub PictureBox7_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox7.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 6 Then
BitmapList.RemoveAt(6)
Path_List.RemoveAt(6)
PictureBox7.Image = Nothing
End If
End Sub
Private Sub PictureBox8_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox8.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 7 Then
BitmapList.RemoveAt(7)
Path_List.RemoveAt(7)
PictureBox8.Image = Nothing
End If
End Sub
Private Sub PictureBox9_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox9.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 8 Then
BitmapList.RemoveAt(8)
Path_List.RemoveAt(8)
PictureBox9.Image = Nothing
End If
End Sub
首先将每个PictureBox
的Tag
属性设置为设计器中的相应数字,即PictureBox1.Tag
为0等。然后您可以将所有这些事件处理程序合二为一:
Private Sub PictureBoxes_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox1.DoubleClick,
PictureBox2.DoubleClick,
PictureBox3.DoubleClick,
PictureBox4.DoubleClick,
PictureBox5.DoubleClick,
PictureBox6.DoubleClick,
PictureBox7.DoubleClick,
PictureBox8.DoubleClick,
PictureBox9.DoubleClick
Dim pb = DirectCast(sender, PictureBox)
Dim n = CInt(pb.Tag)
If BitmapList?.Count > n Then
BitmapList.RemoveAt(n)
Path_List.RemoveAt(n)
pb.Image.Dispose()
pb.Image = Nothing
End If
End Sub
请注意,我添加了一行来处理您要删除的 Image
。如果您还在其他地方使用 Image
对象,请不要这样做,但如果您还没有使用它,一定要这样做。
请注意,我还在其中添加了一个空传播运算符,从而无需进行单独的空检查。
我的表单上有 9 个图片框。每个 PictureBox 都有自己的 DoubleClick 事件处理程序。我还没有找到将这 9 个 subs 合并为一个的方法;特别是因为位图列表 (List(Of System.Drawing.Bitmap)) 必须具有不同数量的 .count
。
如果单击 PictureBox,则只应执行 this 的代码。
Private Sub PictureBox1_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox1.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 0 Then
BitmapList.RemoveAt(0)
Path_List.RemoveAt(0)
PictureBox1.Image = Nothing
End If
End Sub
Private Sub PictureBox2_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox2.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 1 Then
BitmapList.RemoveAt(1)
Path_List.RemoveAt(1)
PictureBox2.Image = Nothing
End If
End Sub
Private Sub PictureBox3_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox3.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 2 Then
BitmapList.RemoveAt(2)
Path_List.RemoveAt(2)
PictureBox3.Image = Nothing
End If
End Sub
Private Sub PictureBox4_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox4.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 3 Then
BitmapList.RemoveAt(3)
Path_List.RemoveAt(3)
PictureBox4.Image = Nothing
End If
End Sub
Private Sub PictureBox5_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox5.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 4 Then
BitmapList.RemoveAt(4)
Path_List.RemoveAt(4)
PictureBox5.Image = Nothing
End If
End Sub
Private Sub PictureBox6_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox6.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 5 Then
BitmapList.RemoveAt(5)
Path_List.RemoveAt(5)
PictureBox6.Image = Nothing
End If
End Sub
Private Sub PictureBox7_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox7.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 6 Then
BitmapList.RemoveAt(6)
Path_List.RemoveAt(6)
PictureBox7.Image = Nothing
End If
End Sub
Private Sub PictureBox8_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox8.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 7 Then
BitmapList.RemoveAt(7)
Path_List.RemoveAt(7)
PictureBox8.Image = Nothing
End If
End Sub
Private Sub PictureBox9_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox9.DoubleClick
If BitmapList IsNot Nothing AndAlso BitmapList.Count > 8 Then
BitmapList.RemoveAt(8)
Path_List.RemoveAt(8)
PictureBox9.Image = Nothing
End If
End Sub
首先将每个PictureBox
的Tag
属性设置为设计器中的相应数字,即PictureBox1.Tag
为0等。然后您可以将所有这些事件处理程序合二为一:
Private Sub PictureBoxes_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox1.DoubleClick,
PictureBox2.DoubleClick,
PictureBox3.DoubleClick,
PictureBox4.DoubleClick,
PictureBox5.DoubleClick,
PictureBox6.DoubleClick,
PictureBox7.DoubleClick,
PictureBox8.DoubleClick,
PictureBox9.DoubleClick
Dim pb = DirectCast(sender, PictureBox)
Dim n = CInt(pb.Tag)
If BitmapList?.Count > n Then
BitmapList.RemoveAt(n)
Path_List.RemoveAt(n)
pb.Image.Dispose()
pb.Image = Nothing
End If
End Sub
请注意,我添加了一行来处理您要删除的 Image
。如果您还在其他地方使用 Image
对象,请不要这样做,但如果您还没有使用它,一定要这样做。
请注意,我还在其中添加了一个空传播运算符,从而无需进行单独的空检查。