Hi I Have 100 Picture Box(PictureBox1-100) And I want to use Each PictureBox with loop like

Dim left as Integer
Do while left > 1
PictureBox 'Picture Box number i used left = 'My Procedure
Left += 1
Loop

How Can I use Each PictureBox From 1 to 100
Thank For Help

Dani AI

Generated

Brief summary and practical options tied to the thread: wants to run the same operation over PictureBox1..PictureBox100. ’s idea to collect controls into a collection is good; an alternative is to discover the PictureBox controls at runtime and produce a numeric-ordered array so iterations behave as expected (avoids lexicographic order like PictureBox1, PictureBox10, PictureBox100, PictureBox2).

A robust runtime-discovery + numeric-sort example (VB.NET):

' gather all PictureBox controls and order by the numeric suffix
Dim pics = Me.Controls.OfType(Of PictureBox)() _
    .Where(Function(pb) pb.Name.StartsWith("PictureBox")) _
    .OrderBy(Function(pb)
                 Dim m = System.Text.RegularExpressions.Regex.Match(pb.Name, "\d+$")
                 Return If(m.Success, Integer.Parse(m.Value), 0)
             End Function) _
    .ToArray()

Practical notes and cautions:

  • Build this collection once (Form_Load) and reuse it; repeated Controls queries inside tight loops hurt performance.
  • Store metadata in each PictureBox.Tag (index, source path, ID) instead of parsing names repeatedly.
  • When replacing images, dispose the old Image first to avoid memory leaks:
If pb.Image IsNot Nothing Then
    Dim old = pb.Image
    pb.Image = Nothing
    old.Dispose()
End If
pb.Image = Image.FromFile(path)
  • UI-thread safety: update PictureBox.Image via Invoke/BeginInvoke if images are loaded on a background thread.
  • If displaying many images causes sluggishness, consider a virtualized control (ListView/DataGridView with an ImageList or owner-drawn panel) rather than 100 separate PictureBoxes.

These steps give predictable ordering, better memory handling, and cleaner runtime management compared with ad-hoc loops.

Hi I Have 100 Picture Box(PictureBox1-100) And I want to use Each PictureBox with loop like

Dim left as Integer
Do while left > 1
PictureBox 'Picture Box number i used left = 'My Procedure
Left += 1
Loop

How Can I use Each PictureBox From 1 to 100
Thank For Help

Create List(Of T).

Dim pboxes As New List(Of PictureBox)

        pboxes.Add(PictureBox1)
        pboxes.Add(PictureBox2)
        pboxes.Add(PictureBox3)

        For Each t As PictureBox In pboxes
            .....
        Next

        'You can access list element through indices

        For i As Integer = 0 To pboxes.Count - 1
            ...
        Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.