I have a problem with a Pong application i want to make in VS2010. The problem is I want to change the colors of the background of the form, the rectangles and the ellipse everytime a timer ticks. My code seems logic, however, when I try to run, my form turns immediately black. The code is shown below.


'needed for dispatchertimer

Imports System.Windows.Threading

Class MainWindow

'private declarations

Private WithEvents tmrTimer As New DispatcherTimer
Private clrColorBackground As New Color
Private clrColorRectangle As New Color
Private clrColorBall As New Color
Private rndRandom As New Random

Private iBr, iBg, iBb, iRr, iRg, iRb, iOr, iOg, iOb As Byte


Private Sub tmrTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick
'subroutines are called within a VS2010 generated method, it's my style of programming
toggleColor()
setColor()
End Sub


Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

'subroutines are called within a VS2010 generated method

defaultColors()

initTimer()

End Sub

Private Sub initTimer()
tmrTimer.Interval = TimeSpan.FromMilliseconds(1000)

tmrTimer.Start()
End Sub

Private Sub defaultColors()
'set default background color

clrColorBackground.R = rndRandom.Next(35, 221)
clrColorBackground.G = rndRandom.Next(35, 221)
clrColorBackground.B = rndRandom.Next(35, 221)

'set default rectangle color

clrColorRectangle.R = rndRandom.Next(35, 221)
clrColorRectangle.G = rndRandom.Next(35, 221)
clrColorRectangle.B = rndRandom.Next(35, 221)

'set default ball color

clrColorBall.R = rndRandom.Next(35, 221)
clrColorBall.G = rndRandom.Next(35, 221)
clrColorBall.B = rndRandom.Next(35, 221)
End Sub

Private Sub toggleColor()

'set red property background
'the following code is repeated for every rgb-part of each control(background, rectangles, ball)

If clrColorBackground.R > 35 And iBr Mod 2 = 0 Then
clrColorBackground.R -= 1
End If

If clrColorBackground.R = 35 Then
iBr += 1
End If

If clrColorBackground.R < 220 And iBr Mod 2 = 1 Then
clrColorBackground.R += 1
End If

If clrColorBackground.R = 220 Then
iBr += 1
End If

'set green property background

If clrColorBackground.G > 35 And iBg Mod 2 = 0 Then
clrColorBackground.G -= 1
End If

If clrColorBackground.G = 35 Then
iBg += 1
End If

If clrColorBackground.G < 220 And iBg Mod 2 = 1 Then
clrColorBackground.G += 1
End If

If clrColorBackground.G = 220 Then
iBg += 1
End If

'set blue property background

If clrColorBackground.B > 35 And iBb Mod 2 = 0 Then
clrColorBackground.B -= 1
End If

If clrColorBackground.B = 35 Then
iBb += 1
End If

If clrColorBackground.B < 220 And iBb Mod 2 = 1 Then
clrColorBackground.B += 1
End If

If clrColorBackground.B = 220 Then
iBb += 1
End If

'set red property rectangle

If clrColorRectangle.R > 35 And iRr Mod 2 = 0 Then
clrColorRectangle.R -= 1
End If

If clrColorRectangle.R = 35 Then
iRr += 1
End If

If clrColorRectangle.R < 220 And iRr Mod 2 = 1 Then
clrColorRectangle.R += 1
End If

If clrColorRectangle.R = 220 Then
iRr += 1
End If

'set green property rectangle

If clrColorRectangle.G > 35 And iRg Mod 2 = 0 Then
clrColorRectangle.G -= 1
End If

If clrColorRectangle.G = 35 Then
iRg += 1
End If

If clrColorRectangle.G < 220 And iRg Mod 2 = 1 Then
clrColorRectangle.G += 1
End If

If clrColorRectangle.G = 220 Then
iRg += 1
End If

'set blue property rectangle

If clrColorRectangle.B > 35 And iRb Mod 2 = 0 Then
clrColorRectangle.B -= 1
End If

If clrColorRectangle.B = 35 Then
iRb += 1
End If

If clrColorRectangle.B < 220 And iRb Mod 2 = 1 Then
clrColorRectangle.B += 1
End If

If clrColorRectangle.B = 220 Then
iRb += 1
End If

'set red property ball


If clrColorBall.R > 35 And iOr Mod 2 = 0 Then
clrColorBall.R -= 1
End If

If clrColorBall.R = 35 Then
iOr += 1
End If

If clrColorBall.R < 220 And iOr Mod 2 = 1 Then
clrColorBall.R += 1
End If

If clrColorBall.R = 220 Then
iOr += 1
End If

'set green property ball

If clrColorBall.G > 35 And iOg Mod 2 = 0 Then
clrColorBall.G -= 1
End If

If clrColorBall.G = 35 Then
iOg += 1
End If

If clrColorBall.G < 220 And iOg Mod 2 = 1 Then
clrColorBall.G += 1
End If

If clrColorBall.G = 220 Then
iOg += 1
End If

'set blue property ball

If clrColorBall.B > 35 And iOb Mod 2 = 0 Then
clrColorBall.B -= 1
End If

If clrColorBall.B = 35 Then
iOb += 1
End If

If clrColorBall.B < 220 And iOb Mod 2 = 1 Then
clrColorBall.B += 1
End If

If clrColorBall.B = 220 Then
iOb += 1
End If

End Sub

Private Sub setColor()
Dim br As New SolidColorBrush

'fill background

br.Color = clrColorBackground
Me.Background = br

'fill ball

br.Color = clrColorBall
ellBall.Fill = br

'fill rectangle

br.Color = clrColorRectangle
rctP1.Fill = br
rctP2.Fill = br

End Sub
End Class