Howto remove multiple Buttons with a loop?

trend

Centurion
Joined
Oct 12, 2004
Messages
171
Hello, got another brain buster (well it is to me :/)

I have 20 buttons on my form:
Code:
Dim button0 As New Button
    Dim button1 As New Button
    Dim button2 As New Button
    Dim button3 As New Button
    Dim button4 As New Button
    Dim button5 As New Button
    Dim button6 As New Button
    Dim button7 As New Button
    Dim button8 As New Button
    Dim button9 As New Button
    Dim button10 As New Button
    Dim button11 As New Button
    Dim button12 As New Button
    Dim button13 As New Button
    Dim button14 As New Button
    Dim button15 As New Button
    Dim button16 As New Button
    Dim button17 As New Button
    Dim button18 As New Button
    Dim button19 As New Button

And I need a simple for look that removes them for my form (the opposite of: Me.Controls.Add(buttonname) )

thanks for any pointers!
Lee
 
If these are the only buttons on the form then you could do something like this. I don't know if modifying a collection while iteration through elements throws an exception. If it does, add all the controls to an arraylist in the foreach and then do another foreach on the arraylist that removes them from the controlcollection.
Visual Basic:
        For Each item As Control In Me.Controls
            If TypeOf item Is Button Then Me.Controls.Remove(item)
        Next
If you do have other buttons, you could give the controls to me removed a tag with the .Tag property (e.x. "Remove") that signifies that they should be removed and do it like this:
Visual Basic:
        For Each Item As Control In Me.Controls
            Try
                If DirectCast(Item.Tag, String) = "Remove" Then Controls.Remove(Item)
            Catch
                'Ignore an exception, most likeley caused by a nonstring .Tag()
            End Try
        Next

[Edit]Sorry, I accidentally put "DirectCast(Item, String)" in the code above.[/Edit]
 
Last edited:
marble_eater said:
If these are the only buttons on the form then you could do something like this. I don't know if modifying a collection while iteration through elements throws an exception. If it does, add all the controls to an arraylist in the foreach and then do another foreach on the arraylist that removes them from the controlcollection.
Visual Basic:
        For Each item As Control In Me.Controls
            If TypeOf item Is Button Then Me.Controls.Remove(item)
        Next
If you do have other buttons, you could give the controls to me removed a tag with the .Tag property (e.x. "Remove") that signifies that they should be removed and do it like this:
Visual Basic:
        For Each Item As Control In Me.Controls
            Try
                If DirectCast(Item, String) = "Remove" Then Controls.Remove(Item)
            Catch
                'Ignore an exception, most likeley caused by a nonstring .Tag()
            End Try
        Next
Thanks guys for the help. I am thinking I don't want to create a control array.. mainly because I would have to rework some of my code (for me .. probably an hour or 2 worth of work :/)

Marble_eater.. I am using this code.. but it just isn't working
Visual Basic:
Public Class Form1
    Inherits System.Windows.Forms.Form


    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim button0 As New Button
        Dim button1 As New Button

        With button0
            .Size = New Size(30, 30)
            .Location = New Point(20, 20)
            .Text = "test"
            .Tag = "Remove"

            'causes the newbtnClick procedure to fire on the click event.
            AddHandler button0.Click, AddressOf button0_Click
        End With
        Me.Controls.Add(button0)


        With button1
            .Size = New Size(30, 30)
            .Location = New Point(60, 60)
            .Text = "test2"
            .Tag = "Remove"
            

            'causes the newbtnClick procedure to fire on the click event.
            AddHandler button1.Click, AddressOf button1_Click
        End With
        Me.Controls.Add(button1)


        For Each Item As Control In Me.Controls
            Try
                If DirectCast(Item, String) = "Remove" Then Controls.Remove(Item)
            Catch
                'Ignore an exception, most likeley caused by a nonstring .Tag( ) 
            End Try
        Next


    End Sub


    Private Sub button0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

End Class

I could use your

Visual Basic:
 For Each item As Control In Me.Controls
            If TypeOf item Is Button Then Me.Controls.Remove(item)
        Next
code.. but I would have to have it look through as many times as there is buttons.



And idea why my code isn't working? because isn't doing anything at all (to remove the buttons)

thanks!
Lee
 
The code I provided didn't work because of a typo. I edited it. Also, you can't remove controls from the collection during the for each or it causes issues, so here is some code that works.

Visual Basic:
        Dim ControlsToRemove As New ArrayList
        For Each Item As Control In Me.Controls
            Try
                If DirectCast(Item.Tag, String) = "Remove" Then
                    ControlsToRemove.Add(Item)
                End If
            Catch ex As Exception
            End Try
        Next
        For Each Item As Object In ControlsToRemove
            Me.Controls.Remove(DirectCast(item, Control))
        Next
 
Last edited:
Back
Top