Results 1 to 4 of 4

Thread: New value not setting in For each statement

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2019
    Posts
    7

    Question New value not setting in For each statement

    I have this code:

    Code:
            For Each srvr In discord.Guilds
    
                Dim pb As New PictureBox
                pb.Width = 70
                pb.Height = 70
                pb.Left = 10
                Dim topVal as Integer = 30
                pb.Top = topVal
                topVal = +30
                Dim ttImage As Bitmap = Bitmap.FromStream(New MemoryStream(tClient.DownloadData(srvr.IconUrl)))
                pb.BackgroundImage = ttImage
                pb.Tag = srvr.Id.ToString + " | " + srvr.Name
                pb.BackgroundImageLayout = ImageLayout.Zoom
                Me.Controls.Add(pb)
                pb.BringToFront()
    
            Next
    It makes the picturebox and adds it onto the form as I wanted, but the new top value doesn't set and they just stack on top of each other. How can I fix this?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: New value not setting in For each statement

    Debug your code. Read through it line by line as it would be executed. You will see that each time the loop is executed, topVal is getting (re)set to 30, so pb.Top is going to be 30 for all of the PictureBoxes created.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: New value not setting in For each statement

    topVal = -30
    This sets topVal to negative 30.

    topVal = +30
    This sets topVal to positive 30.

    Old style:
    topVal = topVal + 30
    This increments topVal by 30

    New style:
    topVal += 30
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  4. #4
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: New value not setting in For each statement

    Hello

    Put
    Code:
    Dim topVal as Integer = 30
    outside the for each loop and apply the correction passel has given. It should solve your problem.

    regards

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