Setting many values in a For...Next loop and Graphics help
Hi!
Can someone please explain to me why this code doesn't work? Most of the description is in the comments of the code, but what I'm looking for is how to make the points move?
Thanks in advance as always!
vb Code:
Public Class Form1
Dim R As New Random
Dim PtList As New List(Of Point)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Start timer and create 300 random points in a 200x200 rectangle.
Timer1.Start()
For i As Int32 = 0 To 300 Step 1
PtList.Add(New Point(R.Next(0, 200), R.Next(0, 200)))
Next
End Sub
Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
'Draw each point.
Dim G As Graphics = e.Graphics
For Each Point In PtList
G.FillRectangle(Brushes.Black, Point.X, Point.Y, 1, 1)
Next
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
'Create a temporary point, subtract a portion, and thus resetting each point's location.
'This should make each point move back and up 5 pixels per tick.
Dim NextPoint As New Point(20, 20)
For Each Point In PtList
Point.X -= CInt(NextPoint.X / 4)
Point.Y -= CInt(NextPoint.Y / 4)
Next
'I have this code here just to make sure the timer is working correctly.
Static a As Int32 = 0
a += 1
Me.Text = a
Me.Invalidate()
End Sub
End Class
P.S. How can I change the color in these code blocks? ^ I prefer the regular VB 2010 colors.
Re: Setting many values in a For...Next loop and Graphics help
Sorry, found myself in wrong thread.
Re: Setting many values in a For...Next loop and Graphics help
Quote:
Originally Posted by
NinjaNic
vb Code:
For Each Point In PtList
Point.X -= CInt(NextPoint.X / 4)
Point.Y -= CInt(NextPoint.Y / 4)
Next
That doesn't do anything to the elements in the list. To change and access the elements try looping thru them by their index...
Code:
For i = 0 To PtList.Count - 1
PtList(i) = New Point(PtList(i).X - 5, PtList(i).Y - 5)
Next
Quote:
P.S. How can I change the color in these code blocks? ^ I prefer the regular VB 2010 colors.
Yes the colors are aweful, and set by forum software. You could always cry about it in forum feedback and maybe they'll update it.
Re: Setting many values in a For...Next loop and Graphics help
Quote:
Originally Posted by
Edgemeal
That doesn't do anything to the elements in the list. To change and access the elements try looping thru them by their index...
Code:
For i = 0 To PtList.Count - 1
PtList(i) = New Point(PtList(i).X - 5, PtList(i).Y - 5)
Next
Yes the colors are aweful, and set by forum software. You could always cry about it in forum feedback and maybe they'll update it.
Nicely Done. A good read on this would be here: https://books.google.com/books?id=U-...tangles&f=true
Re: Setting many values in a For...Next loop and Graphics help
Thank you! This worked: (+Rep)
Quote:
Code:
Code:
For i = 0 To PtList.Count - 1
PtList(i) = New Point(PtList(i).X - 5, PtList(i).Y - 5)
Next