I'm creating a game of checkers and I'm having a problem showing a class called "Checker". This is the checker:
In the form_load I'm doing this:
The player1Checkers() sub looks like this:
The reason I'm declaring all those new Checker pieces and setting the properties individually is because I couldn't think of how to perform that in a loop. However, the problem is that the checker pieces aren't showing at all. Even if I change the panel1.controls.addrange to the grid(x,y).controls.add(checker) the pieces still won't show up. I don't know where my problem is.
Code:
Public Class Checker
Inherits Panel
Public king As Boolean
Public FillColor As Color
Private clicked As Boolean = False
Private Sub Checker_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
If clicked Then
clicked = False
Else
clicked = True
End If
Me.Refresh()
End Sub
Private Sub Checker_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim newBrush As New SolidBrush(FillColor)
Dim rect As Rectangle = Me.DisplayRectangle
rect.Inflate(-1, -1)
e.Graphics.FillEllipse(newBrush, rect)
e.Graphics.DrawEllipse(Pens.Black, rect)
If IsKing() Then
e.Graphics.DrawString("K", Me.Font, New SolidBrush(Color.Black), New Point(CInt(Me.Width / 2 - Me.Font.Size), CInt(Me.Height / 2 - Me.Font.Size)))
End If
If IsClicked() Then
e.Graphics.DrawRectangle(Pens.Red, rect)
End If
End Sub
Public Function IsKing() As Boolean
If king Then
Return True
Else
Return False
End If
End Function
Private Function IsClicked() As Boolean
If clicked Then
Return True
Else
Return False
End If
End Function
End Class
Code:
'Private p1CheckerColor As Color
'Private p2CheckedColor As Color
'Private playing As Boolean = False
'Private pvp As Boolean
'Private grid(7, 7) As Panel
'Those are all declared at the form level
Dim pnl As Panel
Dim row As Integer = 0
Dim col As Integer = 0
Dim check1Color As Color
Dim check2Color As Color
Dim check1 As Boolean = True
'Check if it's pvp or pvc
If My.Settings.playerCount = 0 Then
pvp = False
Else
pvp = True
End If
'Set the board and chip color
If My.Settings.checkerColor = 0 Then
p1CheckerColor = Color.White
p2CheckedColor = Color.Black
check1Color = Color.BurlyWood
check2Color = Color.Tan
Else
p1CheckerColor = Color.White
p2CheckedColor = Color.Red
check1Color = Color.WhiteSmoke
check2Color = Color.RosyBrown
End If
'Add board grid
For i As Integer = 0 To 63
pnl = New Panel
With pnl
'Set the size and the borderstyle
.Size = New Size(74, 74)
.BorderStyle = BorderStyle.FixedSingle
If row = 0 Then
.Location = New Point(0, col * 75)
Else
.Location = New Point(row * 75, col * 75)
End If
'Make the grid checkered
If check1 Then
.BackColor = check1Color
check1 = False
Else
.BackColor = check2Color
check1 = True
End If
'Name the panel
.Name = "pnl" & i.ToString
End With
AddHandler pnl.Click, AddressOf pnl_Click
'Panel one is the board that holds the individual checkers
Panel1.Controls.Add(pnl)
grid(row, col) = pnl
'If the row is less than 7 then add another row, if it's 7 then set the row to 0 and add the col count by 1
If row < 7 Then
row += 1
Else
row = 0
col += 1
End If
Next
Call player1Checkers()
Code:
Private Sub player1Checkers()
'Add p1 pieces
Dim p1Checker1, p1Checker2, p1Checker3, p1Checker4, p1Checker5, p1Checker6, p1Checker7, p1Checker8, p1Checker9, p1Checker10, p1Checker11, p1Checker12 As New Checker
With p1Checker1
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(0, 5).Location
End With
With p1Checker2
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(0, 7).Location
End With
With p1Checker3
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(1, 6).Location
End With
With p1Checker4
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(2, 5).Location
End With
With p1Checker5
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(2, 7).Location
End With
With p1Checker6
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(3, 6).Location
End With
With p1Checker7
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(4, 5).Location
End With
With p1Checker8
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(4, 7).Location
End With
With p1Checker9
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(5, 6).Location
End With
With p1Checker10
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(6, 5).Location
End With
With p1Checker11
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(6, 7).Location
End With
With p1Checker12
.Size = New Size(74, 74)
.FillColor = p1CheckerColor
.Location = grid(7, 6).Location
End With
Panel1.Controls.AddRange({p1Checker1, p1Checker2, p1Checker3, p1Checker4, p1Checker5, p1Checker6, p1Checker7, p1Checker8, p1Checker9, p1Checker10, p1Checker11, p1Checker12})
End Sub