微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法简化这段代码? Visual Basic 在这里

如何解决有没有办法简化这段代码? Visual Basic 在这里

我是 Visual Basic 的新手,这里有一段一直困扰着我的代码。我想不出另一种方法来简化这个。你能帮忙吗?谢谢!:

Private Sub PictureBox_Click(sender As Object,e As EventArgs) Handles PictureBox1.Click,PictureBox2.Click,PictureBox3.Click,PictureBox4.Click,PictureBox5.Click,PictureBox6.Click,PictureBox7.Click,PictureBox8.Click,PictureBox9.Click,PictureBox10.Click,PictureBox11.Click,PictureBox12.Click,PictureBox13.Click,PictureBox14.Click,PictureBox15.Click,PictureBox16.Click,PictureBox17.Click,PictureBox18.Click,PictureBox19.Click,PictureBox20.Click,PictureBox21.Click,PictureBox22.Click,PictureBox23.Click,PictureBox24.Click,PictureBox25.Click,PictureBox26.Click,PictureBox27.Click,PictureBox28.Click,PictureBox29.Click,PictureBox30.Click,PictureBox31.Click,PictureBox32.Click,PictureBox33.Click,PictureBox34.Click,PictureBox35.Click,PictureBox36.Click,PictureBox37.Click,PictureBox38.Click,PictureBox39.Click,PictureBox40.Click,PictureBox41.Click,PictureBox42.Click,PictureBox43.Click,PictureBox44.Click,PictureBox45.Click,PictureBox46.Click,PictureBox47.Click,PictureBox48.Click,PictureBox49.Click,PictureBox50.Click,PictureBox51.Click,PictureBox52.Click,PictureBox53.Click,PictureBox54.Click,PictureBox55.Click,PictureBox56.Click,PictureBox57.Click,PictureBox58.Click,PictureBox59.Click,PictureBox60.Click,PictureBox61.Click,PictureBox62.Click,PictureBox63.Click,PictureBox64.Click
        ...............
        ............... (stuff is down here.)
        ............... 
        ............... 

End Sub

解决方法

您需要将列表存储在某种集合中。这可以是一个数组或一个 List(Of PictureBox),或者如果所有控件都具有相同的父级,您可以简单地利用 Controls 属性。

一旦您有了集合,您将需要遍历集合并使用 AddHandler 进行引用。在引用的方法中,您将获得发送者,并将其转换为 PictureBox。

这是一个假设所有图片框控件都在窗体上的示例:

Private Sub Form1_Load(sender As Object,e As EventArgs)
    For Each pb In Controls.OfType(Of PictureBox)()
        AddHandler pb.Click,AddressOf pb_Click
    Next
End Sub

Private Sub pb_Click(sender As Object,e As EventArgs)
    Dim pb = DirectCast(sender,PictureBox)
    ' pb is the control that was clicked
End Sub
,

这里是如何构建一个图片框列表,它们“按顺序”,假设它们已经在表单上:

Public Class Form1

    Private PBs As New List(Of PictureBox)

    Private Sub Form1_Load(sender As Object,e As EventArgs) Handles MyBase.Load
        For i As Integer = 1 To 64
            Dim ctlName As String = "PictureBox" & i
            Dim ctl As Control = Me.Controls.Find(ctlName,True).FirstOrDefault
            If Not IsNothing(ctl) AndAlso TypeOf ctl Is PictureBox Then
                Dim pb As PictureBox = DirectCast(ctl,PictureBox)
                AddHandler pb.Click,AddressOf PB_Click
                PBs.Add(pb)
            Else
                MessageBox.Show("Unable to find " & ctlName)
            End If
        Next
    End Sub

    Private Sub PB_Click(sender As Object,e As EventArgs)
        Dim pb As PictureBox = DirectCast(sender,PictureBox)
        ' ... do something with pb ...
    End Sub

End Class

来自评论:

等一下,我如何在 Controls.OfType(Of PictureBox)() AddHandler pb.Click,AddressOf pb_Click Next 但是对于一个 List(Of Picturebox)命名为“公式”?

For Each pb As PictureBox in Formula
    AddHandler pb.Click,AddressOf pb_Click
Next

以下是您在打开 BorderStyle 后代码生成的内容:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。