Ok. It was driving me nuts that I didn't know what was going on, so I went and figured out what was going on...
now this program may not be the best programming practices, but it works... I am not used to doing any type of animation, but it appeared like you were trying to do just that so i left your program 'animating'.
Basically, whenever you do PictureBox.createGraphics() you are drawing directly to the control (not inside the Picturebox1.BackgroundImage or .Image). Whenever the form invalidates it repaints and the graphics you drew on it are cleared off (because they are only temporary). Now if you were animating, you would constantly repainting and it would be fine, but since we are just drawing these colors on the form I decided to animate with an object set to Picturebox1.createGraphics() and do the exact same thing onto the Gr object so I could store the graphics we drew into a Bitmap in memory. Then every time the timer is called it resets picturebox1.backgroundimage to that bitmap object of what we have drawn so far so it won't lost it on repaint...
I changed the code ever so slightly, but it should work for you (I moved the Timer1.Enabled = False / True to outside of the DoMain function to increase performance). I also changed the Color <> Color if statements to .Equals because I have Visual Studio 2003 and the <> operator was not working for me.
Enjoy:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim _exit As Boolean = False
Dim Timer1 As New System.Timers.Timer
Dim bitmap As bitmap
Dim GR As Graphics
Dim redBrush As New SolidBrush(Color.Red)
Dim p As Pen = New Pen(Drawing.Color.White, 1)
Dim theColor As Color = Drawing.Color.White
Dim angle, A, B, many, effect, ecount, muchbl, drwidth As Integer
Dim Xmin, Xmax, Ymin, Ymax, UC, pStatus, whButton As Integer
Dim Ux, Uy As Single
Dim Ran As New Random
Dim UclA(11) As Color
Dim clA() As Color = _
{Color.AliceBlue, Color.AntiqueWhite, Color.Aqua, Color.Aquamarine, _
Color.Azure, Color.Beige, Color.Bisque, Color.Black, _
Color.BlanchedAlmond, _
Color.Blue, Color.BlueViolet, Color.Brown, Color.BurlyWood, _
Color.CadetBlue, Color.Chartreuse, Color.Chocolate, Color.Coral, _
Color.CornflowerBlue, Color.Cornsilk, Color.Crimson, Color.Cyan, _
Color.DarkBlue, Color.DarkCyan, Color.DarkGoldenrod, Color.DarkGray, _
Color.DarkGreen, Color.DarkKhaki, Color.DarkMagenta, Color.DarkOliveGreen, _
Color.DarkOrange, Color.DarkOrchid, Color.DarkOrchid, Color.DarkRed, _
Color.DarkSalmon, Color.DarkSeaGreen, Color.DarkSlateBlue, Color.DarkTurquoise, _
Color.DarkViolet, Color.DeepPink, Color.DeepSkyBlue, Color.DimGray, _
Color.DodgerBlue, Color.Firebrick, Color.FloralWhite, Color.ForestGreen, _
Color.Fuchsia, Color.Gainsboro, Color.GhostWhite, Color.Gold, _
Color.Goldenrod, Color.Gray, Color.Green, Color.GreenYellow, _
Color.Honeydew, Color.HotPink, Color.IndianRed, Color.Ivory, _
Color.Khaki, Color.Lavender, Color.LavenderBlush, Color.LawnGreen, _
Color.LemonChiffon, Color.LightBlue, Color.LightCoral, Color.LightCyan, _
Color.LightGoldenrodYellow, Color.LightGray, Color.LightGreen, Color.LightPink, _
Color.LightSalmon, Color.LightSeaGreen, Color.LightSkyBlue, Color.LightSkyBlue, _
Color.LightSlateGray, Color.LightSteelBlue, Color.LightYellow, Color.Lime, _
Color.LimeGreen, Color.Linen, Color.Magenta, Color.Maroon, _
Color.MediumAquamarine, Color.MediumBlue, Color.MediumOrchid, Color.MediumPurple, _
Color.MediumSeaGreen, Color.MediumSlateBlue, Color.MediumSpringGreen, Color.MediumTurquoise, _
Color.MediumVioletRed, Color.MidnightBlue, Color.MintCream, Color.MistyRose, _
Color.Moccasin, Color.NavajoWhite, Color.Navy, Color.OldLace, _
Color.Olive, Color.OliveDrab, Color.Orange, Color.OrangeRed, _
Color.Orchid, Color.PaleGoldenrod, Color.PaleGreen, Color.PaleTurquoise, _
Color.PaleVioletRed, Color.PapayaWhip, Color.PeachPuff, Color.Peru, _
Color.Pink, Color.Plum, Color.PowderBlue, Color.Purple, _
Color.Red, Color.RosyBrown, Color.RoyalBlue, Color.SaddleBrown, _
Color.Salmon, Color.SandyBrown, Color.SeaGreen, Color.SeaShell, _
Color.Sienna, Color.Silver, Color.SkyBlue, Color.SlateBlue, _
Color.SlateGray, Color.Snow, Color.SpringGreen, Color.SteelBlue, _
Color.Tan, Color.Teal, Color.Thistle, Color.Tomato, _
Color.Turquoise, Color.Violet, Color.Wheat, Color.White, _
Color.WhiteSmoke, Color.Yellow, Color.YellowGreen}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler Timer1.Elapsed, AddressOf TimerFired
Randomize()
bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
GR = Graphics.FromImage(bitmap)
GR.Clear(PictureBox1.BackColor)
PictureBox1.BackgroundImage = bitmap
Timer1.Interval = 50 ' 1000 = 1 second
Xmin = 20
Xmax = PictureBox1.Width - 20
Ymin = 20
Ymax = PictureBox1.Height - 20
Ux = Ran.Next(Xmax - Xmin - 40) + Xmin + 20
Uy = Ran.Next(Ymax - Ymin - 40) + Ymin + 20
many = Ran.Next(4991) + 10
theColor = clA(Ran.Next(140))
p.Color = theColor
UC = 0
effect = 1
drwidth = 1
muchbl = 5
pStatus = 0
whButton = 0
ecount = Ran.Next(20) + 1
B = 1
End Sub
Public Sub TimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
Timer1.Enabled = False
For A = 1 To many
DoMain()
Next
Timer1.Enabled = True
PictureBox1.BackgroundImage = bitmap
Select Case whButton
Case 10 'Exit
End
Case 2 'Pause
Timer1.Enabled = False
whButton = 0
Case 3 'Start Over
PictureBox1.CreateGraphics().Clear(Color.Black)
GR.Clear(Color.Black)
whButton = 1
End Select
If B > ecount Then
ecount = Ran.Next(20) + 1
B = 0
End If
B += 1
If UC > 0 Then
theColor = UclA(Ran.Next(UC) + 1)
Else
theColor = clA(Ran.Next(140))
End If
drwidth = 1
'If ((Int((muchbl - 1 + 1) * Rnd() + 1)) - 1) = 3 Then
' ' A 1 in muchbl chance of using Black
' theColor = Color.Black
' drwidth = 8
'End If
p.Color = theColor
many = Ran.Next(4991) + 10
effect = Ran.Next(2) + 1
ecount = Ran.Next(20) + 1
End Sub
Private Sub DoMain()
angle = Ran.Next(8) + 1
Select Case angle
Case 1
Uy -= 1
Case 2
Ux += 1
Uy -= 1
Case 3
Ux += 1
Case 4
Ux += 1
Uy += 1
Case 5
Uy += 1
Case 6
Ux -= 1
Uy += 1
Case 7
Ux -= 1
Case 8
Ux -= 1
Uy -= 1
End Select
If Ux < Xmin Or Ux > Xmax Or Uy < Ymin Or Uy > Ymax Then
Ux = Ran.Next(Xmax - Xmin - 40) + Xmin + 20
Uy = Ran.Next(Ymax - Ymin - 40) + Ymin + 20
End If
Dim TEMPG As Graphics
TEMPG = PictureBox1.CreateGraphics()
TEMPG.DrawEllipse(p, Ux, Uy, 1, drwidth)
GR.DrawEllipse(p, Ux, Uy, 1, drwidth)
Select Case effect
Case 1
Case 2
If Not bitmap.GetPixel(Ux, Uy - 1).Equals(theColor) Then
TEMPG.DrawEllipse(Pens.Black, Ux, Uy - 1, 1, drwidth)
GR.DrawEllipse(Pens.Black, Ux, Uy - 1, 1, drwidth)
ElseIf Not bitmap.GetPixel(Ux + 1, Uy - 1).Equals(theColor) Then
TEMPG.DrawEllipse(Pens.Black, Ux + 1, Uy - 1, 1, drwidth)
GR.DrawEllipse(Pens.Black, Ux + 1, Uy - 1, 1, drwidth)
ElseIf Not bitmap.GetPixel(Ux + 1, Uy).Equals(theColor) Then
TEMPG.DrawEllipse(Pens.Black, Ux + 1, Uy, 1, drwidth)
GR.DrawEllipse(Pens.Black, Ux + 1, Uy, 1, drwidth)
ElseIf Not bitmap.GetPixel(Ux + 1, Uy + 1).Equals(theColor) Then
TEMPG.DrawEllipse(Pens.Black, Ux + 1, Uy + 1, 1, drwidth)
GR.DrawEllipse(Pens.Black, Ux + 1, Uy + 1, 1, drwidth)
ElseIf Not bitmap.GetPixel(Ux, Uy + 1).Equals(theColor) Then
TEMPG.DrawEllipse(Pens.Black, Ux, Uy + 1, 1, drwidth)
GR.DrawEllipse(Pens.Black, Ux, Uy + 1, 1, drwidth)
ElseIf Not bitmap.GetPixel(Ux - 1, Uy + 1).Equals(theColor) Then
TEMPG.DrawEllipse(Pens.Black, Ux - 1, Uy + 1, 1, drwidth)
GR.DrawEllipse(Pens.Black, Ux - 1, Uy + 1, 1, drwidth)
ElseIf Not bitmap.GetPixel(Ux - 1, Uy).Equals(theColor) Then
TEMPG.DrawEllipse(Pens.Black, Ux - 1, Uy, 1, drwidth)
GR.DrawEllipse(Pens.Black, Ux - 1, Uy, 1, drwidth)
ElseIf Not bitmap.GetPixel(Ux - 1, Uy - 1).Equals(theColor) Then
TEMPG.DrawEllipse(Pens.Black, Ux - 1, Uy - 1, 1, drwidth)
GR.DrawEllipse(Pens.Black, Ux - 1, Uy - 1, 1, drwidth)
End If
Case 3
End Select
'PictureBox1.Image = bitmap
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
pStatus = 1
whButton = 1
Timer1.Enabled = True
End Sub
Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
If whButton = 0 Then
Timer1.Enabled = True
whButton = 1
Else
whButton = 2
End If
End Sub
Private Sub btnStartOver_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartOver.Click
whButton = 3
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
If Timer1.Enabled Then
whButton = 10
Else
End
End If
End Sub
End Class