-
Jan 10th, 2015, 10:54 PM
#1
Thread Starter
Addicted Member
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.
-
Jan 11th, 2015, 01:04 AM
#2
Member
Re: Setting many values in a For...Next loop and Graphics help
Sorry, found myself in wrong thread.
-
Jan 11th, 2015, 02:05 AM
#3
Re: Setting many values in a For...Next loop and Graphics help
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
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.
Last edited by Edgemeal; Jan 11th, 2015 at 02:16 AM.
Reason: re-word
-
Jan 11th, 2015, 04:02 AM
#4
Member
Re: Setting many values in a For...Next loop and Graphics help
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
-
Jan 11th, 2015, 10:27 PM
#5
Thread Starter
Addicted Member
Re: Setting many values in a For...Next loop and Graphics help
Thank you! This worked: (+Rep)
Code:
Code:
For i = 0 To PtList.Count - 1
PtList(i) = New Point(PtList(i).X - 5, PtList(i).Y - 5)
Next
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|