Results 1 to 5 of 5

Thread: Setting many values in a For...Next loop and Graphics help

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    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:
    1. Public Class Form1
    2.  
    3.     Dim R As New Random
    4.     Dim PtList As New List(Of Point)
    5.  
    6.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    7.         'Start timer and create 300 random points in a 200x200 rectangle.
    8.         Timer1.Start()
    9.         For i As Int32 = 0 To 300 Step 1
    10.             PtList.Add(New Point(R.Next(0, 200), R.Next(0, 200)))
    11.         Next
    12.     End Sub
    13.  
    14.     Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    15.         'Draw each point.
    16.         Dim G As Graphics = e.Graphics
    17.         For Each Point In PtList
    18.             G.FillRectangle(Brushes.Black, Point.X, Point.Y, 1, 1)
    19.         Next
    20.     End Sub
    21.  
    22.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    23.         'Create a temporary point, subtract a portion, and thus resetting each point's location.
    24.         'This should make each point move back and up 5 pixels per tick.
    25.         Dim NextPoint As New Point(20, 20)
    26.         For Each Point In PtList
    27.             Point.X -= CInt(NextPoint.X / 4)
    28.             Point.Y -= CInt(NextPoint.Y / 4)
    29.         Next
    30.         'I have this code here just to make sure the timer is working correctly.
    31.         Static a As Int32 = 0
    32.         a += 1
    33.         Me.Text = a
    34.         Me.Invalidate()
    35.     End Sub
    36.  
    37. End Class
    P.S. How can I change the color in these code blocks? ^ I prefer the regular VB 2010 colors.

  2. #2
    Member
    Join Date
    Dec 2014
    Posts
    57

    Re: Setting many values in a For...Next loop and Graphics help

    Sorry, found myself in wrong thread.

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Setting many values in a For...Next loop and Graphics help

    Quote Originally Posted by NinjaNic View Post
    vb Code:
    1. For Each Point In PtList
    2.             Point.X -= CInt(NextPoint.X / 4)
    3.             Point.Y -= CInt(NextPoint.Y / 4)
    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

  4. #4
    Member
    Join Date
    Dec 2014
    Posts
    57

    Re: Setting many values in a For...Next loop and Graphics help

    Quote Originally Posted by Edgemeal View Post
    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

  5. #5

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    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
  •  



Click Here to Expand Forum to Full Width