INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

For Each Statement doesn't access all controls

For Each Statement doesn't access all controls

(OP)
Hello everyone,
I am having a picture box on my form named pb and I am adding buttons through runtime, however I need a process for removing all buttons that belong to the pb collection, but for each statement removes one less object-button. Any suggestions please??? Any help would me much appreciated. Thank you so much in advanced.

Public Class Form1
Dim Btn1 As New Button
Dim btn2 As New Button
Dim btn3 As New Button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Btn1.SetBounds(100, 200, 76, 27)
Btn1.Name = "button1"
btn2.BackColor = Color.Aqua
btn2.SetBounds(50, 100, 76, 27)
btn2.Name = "button2"
btn3.BackColor = Color.Green
btn3.SetBounds(300, 100, 76, 27)
btn3.Name = "button3"
pb.Controls.Add(Btn1)
pb.Controls.Add(btn2)
pb.Controls.Add(btn3)
End Sub

Private Sub Remove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Remove.Click
For Each obj As Button In Me.pb.Controls
pb.Controls.Remove(obj)
Next


End Sub

RE: For Each Statement doesn't access all controls


This will remove all the buttons in the picture box:

CODE

For i As Integer = PictureBox1.Controls.Count - 1 To 0 Step -1

    If TypeOf PictureBox1.Controls(i) Is Button Then

        PictureBox1.Controls.Remove(PictureBox1.Controls(i))

    End If

Next 

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: For Each Statement doesn't access all controls

The problem with the original code was that "pb.Controls" is a collection. You want to avoid modifying a collection as you're walking through it in a for/each loop or you end up with unexpected results, as you saw.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close