Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello, got another brain buster (well it is to me :/)

 

I have 20 buttons on my form:

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

  • Leaders
Posted (edited)

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.

       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:

       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]

Edited by snarfblam
[sIGPIC]e[/sIGPIC]
Posted
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.

       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:

       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

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

 

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

  • Leaders
Posted (edited)

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.

 


       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

Edited by snarfblam
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...