For Each

random controlsAll of the controls on a form makeup a collection. The For Each loop can be used to loop through each object in a collection.
Start a new application and place a few controls of various types on the form.
Write the code to arrange the controls in Form Load:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim myControl As Control
  Dim Y As Integer
  For Each myControl In Me.Controls
      myControl.SetBounds(0, Y, myControl.Width, myControl.Height)
      Y += myControl.Height + 10
  Next myControl
End Sub 'Form1_Load
arrangedAt run time the controls are arranged neatly.
INDEX, A Demo loop program, For Loops, Step: change the increment, While Loops, While, cont., Endless loops, Do Loops, Nested Loops, For Each Loops, Graphics with Loops
Next lesson: Arrays in Visual Basic

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

For Each Loops