The approach you're using is bound to have problems. You might want to do away with the PictureBox. You can use the PictureBox as a drawing container, but it won't really help unless you have a complicated user interface and need the docking/anchoring behavior for proper layout and positioning. I'll explain how you can do the drawing directly on the Form.
For starters, things will look their best if the form is double-buffered, so I recommend you set your form's DoubleBuffered property to true. As a general rule, and especially when double-buffering, you want to do all of your drawing in the form's Paint event. This is simple enough to set up:
Code:
[COLOR="Red"]Private Sub Form_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
' Draw here, using the e.Graphics object
End Sub[/COLOR]
Now, when you draw, the drawing surface may or may not be cleared automatically. This depends on whether or not the form has it's opaque style flag set. I think by default, the drawing surface will be cleared automatically, but you'll want to double-check (or, you'll find anyways out when you try to draw your moving circle).
I gather that you know how to draw a circle, but I'll throw the code here for completeness.
Code:
[COLOR="Red"]Dim CircleLocation As Point
Const CircleDiameter As Integer = 750[/COLOR]
Private Sub Form_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
[COLOR="Red"] Dim circleBounds As New Rectangle(CircleLocation.X, CircleLocation.Y, CircleDiameter, CircleDiameter)
e.Graphics.FillEllipse(Color.HotPink, circleBounds)[/COLOR]
End Sub
The location the circle will be drawn at is specified by CircleLocation. To move the circle, we need to change the value of CircleLocation and re-draw the circle. We would do this in a loop or with a timer in your case.
Updating CircleLocation is easy enough:
Code:
[COLOR="Red"]' Move right one pixel
CircleLocation.Offset(1, 0)
' Move up two pixels
CircleLocation.Offset(0, -2)[/COLOR]
The process of redrawing requires some explanation. When you do your drawing in the Paint event, you don't directly call the drawing code. Instead, you
invalidate the form or control that needs to be drawn. Invalidation tells Windows that something needs to be drawn, and Windows calls your Paint event for you.
You can invalidate the entire form, to redraw the whole thing...
Code:
' Move up two pixels
CircleLocation.Offset(0, -2)
[COLOR="Red"]Me.Invalidate()[/COLOR]
Or, you can specify a rectangle to redraw, which requires more effort, but gives better performance
Code:
' Move up two pixels
[COLOR="Red"]Dim oldRect As New Rectangle _
(CircleLocation.X, CircleLocation.Y, CircleDiameter, CircleDiameter)
[/COLOR]CircleLocation.Offset(0, -2)
[COLOR="Red"]Dim newRect As New Rectangle _
(CircleLocation.X, CircleLocation.Y, CircleDiameter, CircleDiameter)
' We will redraw the area containing both the old and new locations of the circle
Dim invalidRect As Rectangle = Rectangle.Union(oldRect, newRect)
Me.Invalidate(invalidRect)[/COLOR]
There is still one problem, though. When you invalidate a control, there is a tiny delay before it is redrawn. The reason for this is to let Windows make things more efficient. If you invalidate more than one thing in a small amount of time, waiting a tiny bit gives Windows the chance to combine all the drawing into a single Paint event. Normally this is a good thing, but you don't want to combine your Paint events, because that would ruin your smooth animation.
The solution is easy. If you call the Update method on a control or a form, it will be drawn immediately without delay.
Code:
' Move up two pixels
Dim oldRect As New Rectangle _
(CircleLocation.X, CircleLocation.Y, CircleDiameter, CircleDiameter)
CircleLocation.Offset(0, -2)
Dim newRect As New Rectangle _
(CircleLocation.X, CircleLocation.Y, CircleDiameter, CircleDiameter)
' We will redraw the area containing both the old and new locations of the circle
Dim invalidRect As Rectangle = Rectangle.Union(oldRect, newRect)
Me.Invalidate(invalidRect)
[COLOR="Red"]Me.Update()[/COLOR]
That should do the trick. I took a wild guess and hoped you wanted VB, but if you program in C#, you should still be able to understand it, and if you do program in VB, be warned that there are probably syntax or other minor errors in the code; I did not type any of it into the IDE. As long as you understand the code, you should have no problem getting it to work.