Looping through Controls in VB.NET

前端 未结 4 1626
南笙
南笙 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:42

    Generating controls at design time via the Forms Designer only makes sense for layouts which benefit from the forms designer.

    In your case, you just have 64 uniform boxes in 8 rows of 8. Don’t use the Forms Designer for this, create the controls at runtime, and don’t give them names like pba1, just put them into an appropriate data structure (such as an 8x8 array):

    Private chessFields As PictureBox(8, 8)
    
    ' In Form_Load:
    For i = 0 To 7
        For j = 0 To 7
            chessFields(i, j) = New PictureBox
            ' Set size, position … then, finally,
            Controls.Add(chessFields(i, j))
        Next
    Next
    

    That way, you can access the fields in an orderly fashion without having to go via the Form.Controls collection.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-22 18:58

    Loop through the PictureBox's in your ControlCollection and test for BackColor. I used the Form's ControlCollection, if they are in some other type of container control use that.

    For Each cntrl As Control In Me.Controls
        If TypeOf cntrl Is PictureBox Then
            If cntrl.BackColor = Color.Black Then
                'Do Something
            End If
        End If
    Next
    

    Base on the additional information that you gave in your answer, the reason your example is not working is that the Controls Name is a String and you are comparing it to the PictureBox Control not the Name of the Control.

    You can try using the Tag Property instead of the Name of the Control, it will be cleaner and easier to read. I just put a 1 in the PictureBox's Tag Property for Black and a 0 for White.

    Private Sub OriginalColour()               
        For Each cntrl As Control In Me.Controls
            Dim result As Integer
            If TypeOf cntrl Is PictureBox Then
                If Integer.TryParse(cntrl.Tag.ToString, result) Then
                    If result = 1 Then
                        cntrl.BackColor = Color.Gray
                    Else
                        cntrl.BackColor = Color.White
                    End If
                End If
    
            End If
        Next
    End Sub
    
    0 讨论(0)
  • 2021-01-22 19:07

    Put all the pictureboxes in an 8x8 tableLayoutPanel (also useful for scaling etc). Then

        For Each pb As PictureBox In TableLayoutPanel1.Controls
            Dim col As Integer = TableLayoutPanel1.GetCellPosition(pb).Column
            Dim row As Integer = TableLayoutPanel1.GetCellPosition(pb).Row
            If col Mod 2 = 0 Xor row Mod 2 = 0 Then
                pb.BackColor = Color.Black
            Else
                pb.BackColor = Color.White
            End If
        Next
    

    Of course you could also use an array of the squares if you have that available.

    This will not affect the events (pba1.click etc).

    0 讨论(0)
提交回复
热议问题