for example i have 2000 picture box and i have a array the lenght of 2000.
and my picturebox names is like pb1,pb2 .... pb1999, pb2000

so if my arrays 1393th item is not null i want to change 1393th pictureboxs color.

is there any easy way?

i know i can use select case but if i use this it takes 2000 row and its useless :D

thx for read

Dani AI

Generated

The simplest safe approach is not to hardcode 2000 Select Case lines. As hinted, iterate by index — but use a fast lookup for the control rather than trying to spell a control name into code. As found, grouping picture boxes in a container helps, but there are better options.

Lookup by name (search children) — quick to implement:

Dim name = "pb" & i.ToString()
Dim matches() As Control = Me.Controls.Find(name, True)   ' True searches child controls
If matches.Length > 0 Then
    Dim pb = TryCast(matches(0), PictureBox)
    If pb IsNot Nothing Then pb.BackColor = Color.Red
End If

Faster at runtime: build one index (dictionary) once and reuse it. This avoids repeated tree searches:

' Build once (Form.Load)
Dim pbMap As New Dictionary(Of Integer, PictureBox)
For Each pb As PictureBox In Me.Controls.OfType(Of PictureBox)()
    Dim n As Integer
    If Integer.TryParse(pb.Name.Substring(2), n) Then pbMap(n) = pb
Next

' Later, change by index quickly
If array(i) IsNot Nothing AndAlso pbMap.ContainsKey(i) Then pbMap(i).BackColor = Color.Red

Notes and cautions:

  • Avoid calling Controls.Find inside tight loops many times; build a map or use the Tag property when you create controls to store the index for O(1) checks.
  • 2000 PictureBox controls can be heavy: layout, painting and memory cost may be noticeable. Consider virtualized UI (DataGridView virtual mode) or an owner-drawn single canvas and draw images on demand instead of thousands of WinForms controls. See the ControlCollection.Find docs for the lookup approach: Control.ControlCollection.Find.

Recommended Answers

All 3 Replies

Why not loop through the items in the array by index..

umm couse i cant use loop
i wish this code but

for i=0 to arry.lenght-1
if arry(i) isnot null then
pb&i.backcolor= red 'i wish this code but :D we cant 
next

hmm i found something
i use panel for all picturebox and for each :))

but i we have 2000 picturebox and some diffrent controls like buttons textboxs and something like this
if we shouldnt use panel for just pictures? how can i do that

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.