Looping through Controls in VB.NET

前端 未结 4 1672
南笙
南笙 2021-01-22 18:04

I am creating a chess program. And it is composed of sixty four picture boxes with alternating black and white background colours.
I have named them pba1,

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-22 18:54

    This is fairly simple and it may be resource heavy, but it works. I have a form with 36 CheckBoxes. This takes advantage of the fact that when you copy a checkbox it just increases the number of the name. I ended up with 36 checkboxes named CheckBox1 thru Checkbox36. The Function returns a checkbox, which may be used to set or read any property.

    Private Function GetCheckBox(ByVal Index As Integer) As CheckBox
        Dim CKBox As checkbox
        For Each cntrl As Control In Me.Controls
            If TypeOf cntrl Is CheckBox Then
                CKBox = cntrl
                If CKBox.Name = "CheckBox" & Index Then
                    Exit For
                End If
            End If
        Next
        Return ckbox
    End Function
    

提交回复
热议问题